I am trying to create a upgrade system for my tower defense game. The script I have now is
localscript:
local player = game.Players.LocalPlayer
local remote = game.ReplicatedStorage.remotes.Billboard
remote.OnClientEvent:Connect(function(tower)
script.Parent.BillboardGui.Adornee = tower
local BG = script.Parent.BillboardGui.BG
local currentlvl = tower[tower.Name].CurrentLvl
local buy = BG.Main.TextButton
local lvl = BG.Main.level
local nextlvl = BG.Main.nextlevel
local towerdata = require(game.ReplicatedStorage.Tower_Data)
local maxlevel = towerdata.Data[tower.Name]["Maxlevel"]
local buylevel = game.ReplicatedStorage.remotes.buyupgrade
local currentvalue = currentlvl.Value
BG.Visible = not BG.Visible
lvl.Text = currentlvl.Value
if currentlvl.Value ~= maxlevel then
currentvalue += 1
nextlvl.Text = currentvalue + 1
else
nextlvl.Text = "Max"
lvl.Text = "Max"
end
buy.MouseButton1Click:Connect(function()
buylevel:FireServer(tower, currentlvl)
lvl.Text = currentlvl.Value
if currentlvl.Value ~= maxlevel then
currentvalue += 1
nextlvl.Text = currentvalue + 1
print("eh")
else
nextlvl.Text = "Max"
lvl.Text = "Max"
end
end)
end)
server:
upgraderemote.OnServerEvent:Connect(function(player, tower, level)
local money = player.leaderstats.Money
local currentlvl = tower[tower.Name].CurrentLvl
local towerdata = require(game.ReplicatedStorage.Tower_Data)
local maxlevel = towerdata.Data[tower.Name]["Maxlevel"]
local cost = towerdata.Data[tower.Name][level.Value]["upgradecost"]
if level.Value ~= maxlevel then
if money.Value >= cost then
currentlvl.Value += 1
money.Value -= cost
end
else
return
end
end)
However, the first time I upgrade a tower it works perfectly (kinda) but the second time it upgrades it to max level with 1 button click as seen in the video below
It looks like the issue is with currentlvl in which you’re looking for the first tower with that name instead of the exact tower you have selected. You can just use the tower argument instead of trying to search for it with its name.
do you mean that instead of tower[tower.Value].CurrentLvl I do tower[tower].CurrentLvl? because that gives the error “invalid argument #2 (string expected, got Instance)”
Edit: this is probably because the tower is in a model named after the tower
so it gives the error when you just use the tower as a variable alone
It’s because you have the functions inside the function (If that makes senses). The moment the first event fires, its will keep allowing the other functions to keep firing. You will need to Disconnect the function, once you don’t want to use them anymore.
local player = game.Players.LocalPlayer
local remote = game.ReplicatedStorage.remotes.Billboard
local function1
remote.OnClientEvent:Connect(function(tower)
script.Parent.BillboardGui.Adornee = tower
local BG = script.Parent.BillboardGui.BG
local currentlvl = tower[tower.Name].CurrentLvl
local buy = BG.Main.TextButton
local lvl = BG.Main.level
local nextlvl = BG.Main.nextlevel
local towerdata = require(game.ReplicatedStorage.Tower_Data)
local maxlevel = towerdata.Data[tower.Name]["Maxlevel"]
local buylevel = game.ReplicatedStorage.remotes.buyupgrade
local currentvalue = currentlvl.Value
BG.Visible = not BG.Visible
lvl.Text = currentlvl.Value
if currentlvl.Value ~= maxlevel then
currentvalue += 1
nextlvl.Text = currentvalue + 1
else
nextlvl.Text = "Max"
lvl.Text = "Max"
end
function1 = buy.MouseButton1Click:Connect(function()
buylevel:FireServer(tower, currentlvl)
lvl.Text = currentlvl.Value
if currentlvl.Value ~= maxlevel then
currentvalue += 1
nextlvl.Text = currentvalue + 1
print("eh")
else
nextlvl.Text = "Max"
lvl.Text = "Max"
end
function1:Disconnect()
end)
end)
What I always try when somethings doesn’t work the way I want it to, is add print(Number), to different lines. This way you can see where you code goes wrong. That’s something to try.