#sync gui with tower upgrade
280 messages · Page 1 of 1 (latest)
** You are now Level 6! **
function upgradeTower(tower, rangePart, hrp, clicked)
local towerName = tower.Name
local upgradeAmount = tower.Upgrade.Value
local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount).Frame
local moneyAmount = upgradeGui.UpgradeAmount.Value
local upgradeButton = upgradeGui.Upgrade
upgradeGui.Visible = true
upgradeButton.MouseButton1Click:Connect(function()
local playerMoney = player.leaderstats.Money.Value
if playerMoney >= moneyAmount then
upgradeGui.Visible = false
local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount, moneyAmount)
upgradeTower(upgradedTower)
end
end)
if rangePart.Parent == hrp and clicked then
upgradeGui.Visible = true
elseif rangePart.Parent ~= hrp and clicked then
upgradeGui.Visible = true
elseif not clicked then
upgradeGui.Visible = false
end
end```
uhh
i dont think you can store .value as a variable
store the object and do .value later
idk what you mean sorry
wait
dont do local upgradeAmount = tower.Upgrade.Value
do local upgradeAmount = tower.Upgrade
i get what you mean but what difference does it make
idk but i had a similar issue before
like you cant store .value as a variable
it makes no sense
but its like that
alr, but it didnt improve much
sure let me see
can u only send the part with the issue
like the line that gives the error
rangepart doesnt exist?
sorry i need to leave
np
but heres the whole script
wait
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local upgradeTowerEvent = events:WaitForChild("UpgradeTower")
local towerFolder = workspace:WaitForChild("Towers")
local players = game:GetService("Players")
local player = players.LocalPlayer
local rangePart = workspace.Utility:WaitForChild("rangePart")
towerFolder.ChildAdded:Connect(function(tower)
local clicked = false;
local hitbox = tower:WaitForChild("Hitbox")
local clickDetector = hitbox.ClickDetector
local hrp = hitbox.Parent.HumanoidRootPart
local radius = hrp.Parent.Range.Value
local debounce = false;
clickDetector.MouseClick:Connect(function()
if debounce then return end
debounce = true
if rangePart.Parent == hrp and clicked then
rangePart.Transparency = 1
clicked = false
debounce = false
upgradeTower(tower, rangePart, hrp, clicked)
return
elseif rangePart.Parent ~= hrp and clicked then
clicked = false
debounce = false
upgradeTower(tower, rangePart, hrp, clicked)
end
if not clicked then
rangePart.Parent = hrp
rangePart.Size = Vector3.new(1, radius * 2, radius * 2)
rangePart.CFrame = (hrp.CFrame * CFrame.new(0, -hrp.Parent.Humanoid.HipHeight - 1, 0)) * CFrame.Angles(0, 0, math.rad(90))
rangePart.Transparency = 0.8
clicked = true
debounce = false
upgradeTower(tower, rangePart, hrp, clicked)
return
end
end)
end)```
function upgradeTower(tower, rangePart, hrp, clicked)
local towerName = tower.Name
local upgradeAmount = tower.Upgrade.Value
local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount).Frame
local moneyAmount = upgradeGui.UpgradeAmount.Value
local upgradeButton = upgradeGui.Upgrade
upgradeGui.Visible = true
upgradeButton.MouseButton1Click:Connect(function()
local playerMoney = player.leaderstats.Money.Value
if playerMoney >= moneyAmount then
upgradeGui.Visible = false
local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount, moneyAmount)
upgradeTower(upgradedTower, rangePart, hrp, clicked)
end
end)
if rangePart.Parent == hrp and clicked then
upgradeGui.Visible = true
elseif rangePart.Parent ~= hrp and clicked then
upgradeGui.Visible = true
elseif not clicked then
upgradeGui.Visible = false
end
end```
here
upgradeTowerEvent.OnServerInvoke = function(player, tower, upgradeAmount, moneyAmount)
tower.Upgrade.Value += 1
local playerMoney = player.leaderstats.Money
playerMoney.Value -= moneyAmount
local indexTower = towerDatabase[tower.Name .. upgradeAmount + 1]
tower.Damage.Value = indexTower.Damage -- error here
tower.Range.Value = indexTower.Range
tower.Firerate.Value = indexTower.Firerate
return tower
end```
and its triggering multiple times when i click the upgrade button
Someone look on studio help classic
@trim hull
indexTower is nil
it doesnt exists
local level = towerLevel.Value --towerLevel.Value returns the value of the container, it doesnt represent the property itself. so typeof(level) would be an int (or number, wtvr object it is)
level = 10 --this changes the variable named "level", not the towerLevel object itself
local towerLev = towerLevel --sets the towerLev variable to the object *instance*
towerLev.Value = 10 --sets the property named 'value' of towerLevel object to 10
thats the difference, so it wouldnt update properly
oh yea
im indexing something that doesnt exist yet
do i just use a debounce
so it doesnt trigger many times
what elements
not about this just when clicking the button
wait ill open roblox studio in a sec
no
the debounce doesnt really do much here
if IndexTower is nil, then there is no element in that array
with that index
i know
so how are elements being added to that array
uh im adding them manually
local towerDatabase = {
["Vandal0"] = {
["Damage"] = 25,
["Firerate"] = 1,
["Range"] = 20
},
["Vandal1"] = {
["Damage"] = 40,
["Firerate"] = 0.8,
["Range"] = 25
}
}```
but i mean when i click the upgrade button once i get like 10 errors
so its triggering multiple times
for some reason :(
these?
yo how do i count the number of keys
i can fix it then
indextower being nil
theres no builtin method
youll have to brute force it
im pretty sure
can you try printing the player's money and the cost amount right before upgrading
then the same thing in the function that actually does it
ye but i still need to send it to the server
you mean doing the math on the client then sending it
no you should do the math on server
i meant print the values on server as well
but it shouldnt really matter
is the 0 print relevant?
oh
ye lua local count = 0 for i, v in pairs(towerDatabase) do if i == tower.Name then count += 1 end end print(count) i did this
but it cant pass the comparison for some reason
ye ye
a recursive call would be better
and this only counts its first scope elements
so array inside array wouldnt be counted
yep i dont need it to be counted
what is that?
oh nevermind then
function calling itself
in that case you can do #array
can you add those prints here
but keep the other prints as well
local tabl = {1,2,3,4,5}
local val = #tabl
print(val)
;compile
5
local tabl = {
['1'] = 'a',
['10'] = 'b'
}
local val = #tabl
print(val)
;compile
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:3: '}' expected (to close '{' at line 1) near '['
oof
** You are now Level 7! **
;compile
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:3: '}' expected (to close '{' at line 1) near '['
u gotta put it in quotes or something
;compile
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:2: '}' expected (to close '{' at line 1) near '='
weird
i dont think so for indexed values
anyways idk whats going on 💀 but it counts the max index not the length
local tabl = {
["1"] = "a"
["10"] = "b"
}
local val = #tabl
print(val)```
;compile
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:3: '}' expected (to close '{' at line 1) near '['
what (
oh forgot the comma 💀
😎
;compile
1
local towerDatabase = {
["Vandal0"] = {
["Damage"] = 25,
["Firerate"] = 1,
["Range"] = 20
},
["Vandal1"] = {
["Damage"] = 40,
["Firerate"] = 0.8,
["Range"] = 25
}
}
local val = #towerDatabase
print(val)
;compile
0
;compile
0
😭
yea how do i count the keys then
i was mistekan 
the loop you used before would work
anyways back to our problem
local towerDatabase = {
["Vandal0"] = {
["Damage"] = 25,
["Firerate"] = 1,
["Range"] = 20
},
["Vandal1"] = {
["Damage"] = 40,
["Firerate"] = 0.8,
["Range"] = 25
}
}
local count = 0
local tower = "Vandal"
for i, v in pairs(towerDatabase) do
if i == tower then
count += 1
end
end
print(count)```
looks like it invokes multiple times
;compile
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:17: syntax error near '+'
;compile
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:18: syntax error near '+'
ugh
yea
oh thats what you meant by add a debounce
but it would be a different connection so it wouldnt be firing twice
nah u still gotta click on the button
except its not a clickdetector
so idk
local count = 0
for i, v in pairs(towerDatabase) do
if i == tower.Name then
count += 1
end
end``` but why isnt this working
it cant pass the if check
even tho the values are the same
i am
wait
local count = 0
for i, v in pairs(towerDatabase) do
if i == tower.Name then
count += 1
end
end
print(count)```
```lua
local towerDatabase = {
["Vandal0"] = {
["Damage"] = 25,
["Firerate"] = 1,
["Range"] = 20
},
["Vandal1"] = {
["Damage"] = 40,
["Firerate"] = 0.8,
["Range"] = 25
}
}```
print i and v every iteration
?
tower.Name is Vandal but in the database its Vandal0 or Vandal1
oh alr
ye so how do i add a number at the end
'Vandal'..i
use ipairs
oh wait
yeha
actually what are u doing?
i dont understand
but use .. to concatanate
nvm it wont work
i gotta remove the number at the end instead
alright fixed it
upgradeTowerEvent.OnServerInvoke = function(player, tower, upgradeAmount, costAmount)
tower.Upgrade.Value += 1
local playerMoney = player.leaderstats.Money
playerMoney.Value -= costAmount
local towerUpgradeLimit = 0
for i, v in pairs(towerDatabase) do
local dbTowerName = i:gsub(".?$","")
if dbTowerName == tower.Name then
towerUpgradeLimit += 1
end
end
if upgradeAmount >= towerUpgradeLimit then
local indexTower = towerDatabase[tower.Name .. upgradeAmount + 1]
tower.Damage.Value = indexTower.Damage
tower.Range.Value = indexTower.Range
tower.Firerate.Value = indexTower.Firerate
end
return tower
end``` this is working
how do i add a debounce?
upgradeButton.MouseButton1Click:Connect(function()
local playerMoney = player.leaderstats.Money
if playerMoney.Value >= costAmount.Value then
upgradeGui.Visible = false
local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount.Value, costAmount.Value)
upgradeTower(upgradedTower, rangePart, hrp, clicked)
return
end
end)
if rangePart.Parent == hrp and clicked then
upgradeGui.Visible = true
elseif rangePart.Parent ~= hrp and clicked then
upgradeGui.Visible = true
elseif not clicked then
upgradeGui.Visible = false
end``` here
the click is triggering multiple times
Does anybody know how to click the character and it will let me buy the character and put it in my inventory
** You are now Level 2! **
Like i click on this dummy and it let's me buy the WHOLE character and it goes into my inventory
Does anybody know how to do that?
create ur own thread for it man
that's not gonna be that easy
Is there a kit for that then?
watch a tutorial or smthn
u can i guess clone the dummy from server storage once u click on it and then add it to your inventory
I did and it's not helping at all
wait he wnats the character in the player backpack as a tool?
.
idk
No
Listen guys
How do you buy a dummy and it adds to ur inventory
Let me find an example
On tiktok
you mean like add it to an inventory youve created?
help pls
just creata a variable for it
local debounce = false
upgradeButton.MouseButtonClick:Connect(function()
if debounce then return end
debounce = true
-- script
task.wait(2)
debounce = false
end)
function upgradeTower(tower, rangePart, hrp, clicked)
local towerName = tower.Name
local upgradeAmount = tower.Upgrade
local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount.Value).Frame
local costAmount = upgradeGui.UpgradeAmount
local upgradeButton = upgradeGui.Upgrade
local debounce = false
upgradeGui.Visible = true
upgradeButton.MouseButton1Click:Connect(function()
if debounce then return end
debounce = true
local playerMoney = player.leaderstats.Money
if playerMoney.Value >= costAmount.Value then
upgradeGui.Visible = false
local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount.Value, costAmount.Value)
upgradeTower(upgradedTower, rangePart, hrp, clicked)
end
task.wait(1)
debounce = false
end)
if rangePart.Parent == hrp and clicked then
upgradeGui.Visible = true
elseif rangePart.Parent ~= hrp and clicked then
upgradeGui.Visible = true
elseif not clicked then
upgradeGui.Visible = false
end
end``` you mean like this
but it still rarely triggers multiple times
yea
.
Finally found it
Sorry it took a while
Like this
For ppl to buy my dummy
And it's in their inventory
And then u find it here
huh
make it print smthn when someone clicks the button to check
like if i try to click more times than one it printed it 4 times
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local upgradeTowerEvent = events:WaitForChild("UpgradeTower")
local towerFolder = workspace:WaitForChild("Towers")
local players = game:GetService("Players")
local player = players.LocalPlayer
local rangePart = workspace.Utility:WaitForChild("rangePart")
towerFolder.ChildAdded:Connect(function(tower)
local clicked = false;
local hitbox = tower:WaitForChild("Hitbox")
local clickDetector = hitbox.ClickDetector
local hrp = hitbox.Parent.HumanoidRootPart
local radius = hrp.Parent.Range.Value
local debounce = false;
clickDetector.MouseClick:Connect(function()
if debounce then return end
debounce = true
if rangePart.Parent == hrp and clicked then
rangePart.Transparency = 1
clicked = false
debounce = false
upgradeTower(tower, rangePart, hrp, clicked)
return
elseif rangePart.Parent ~= hrp and clicked then
clicked = false
debounce = false
upgradeTower(tower, rangePart, hrp, clicked)
end
if not clicked then
rangePart.Parent = hrp
rangePart.Size = Vector3.new(1, radius * 2, radius * 2)
rangePart.CFrame = (hrp.CFrame * CFrame.new(0, -hrp.Parent.Humanoid.HipHeight - 1, 0)) * CFrame.Angles(0, 0, math.rad(90))
rangePart.Transparency = 0.8
clicked = true
debounce = false
upgradeTower(tower, rangePart, hrp, clicked)
return
end
end)
end)
function upgradeTower(tower, rangePart, hrp, clicked)
local towerName = tower.Name
local upgradeAmount = tower.Upgrade
local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount.Value).Frame
local costAmount = upgradeGui.UpgradeAmount
local upgradeButton = upgradeGui.Upgrade
local debounce = false
upgradeGui.Visible = true
upgradeButton.MouseButton1Click:Connect(function()
if debounce then return end
debounce = true
local playerMoney = player.leaderstats.Money
if playerMoney.Value >= costAmount.Value then
upgradeGui.Visible = false
local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount.Value, costAmount.Value)
print("clicked")
upgradeTower(upgradedTower, rangePart, hrp, clicked)
task.wait(1)
debounce = false
return
end
task.wait(1)
debounce = false
return
end)
if rangePart.Parent == hrp and clicked then
upgradeGui.Visible = true
elseif rangePart.Parent ~= hrp and clicked then
upgradeGui.Visible = true
elseif not clicked then
upgradeGui.Visible = false
end
end```
here
clicked once lol
@vestal oar
Is the print for an mb1 or the actual invoke?
I don’t think it’s being clicked multiple times, unless you spam it
The problem is that one click invokes the server multiple times, resulting in multiple money subtractions right?
I don’t think it’s possible for the mb1 event to fire like that because it’s mb1 down and up
Basically, it can only fire once for every click
Hey i made a new thread btw https://discord.com/channels/448986884497211392/1280502232839880746
And wtf is this
Are u dumb
no 💀
I didn’t want to read
Then don't say shit then
lmao
😂😂
Anyways
That’s not what I was talking about
If u have a problem u can make your own post
U almost crashed my phone cuz I had to scroll so much 🙄🙄🙄
The problem is that I did and nobody answered
Crazy
o yea that sucks
Happens a lot
Yh
If u repost some people are bound to see it
It’s just kinda rude to come in to other posts
How bru 💀
I js came here bcz no one answerd my posts and since some ppl were typing here I js said it here
I actually can’t think of a valid reason lmao but idk it just feels rude
Yh for u
Girlie pop
** You are now Level 3! **