Upgrade button activates multiple times

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

can someone help me with this?

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
Screenshot_96
so it gives the error when you just use the tower as a variable alone

I mean to just use tower alone, assuming tower is a variable referencing the tower model itself.

I changed the code to what you said and did some other tweeks to make it work again however, now it upgrades with 2 at a time when I upgrade it once

the code used now is

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.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)
	end)
	
	currentlvl.Changed:Connect(function()
		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)

This part here doesn’t make much sense to me. You’ve got an event that will give you an updated value, but you add on top of that.

It should be something more like:

currentlvl:GetPropertyChangedSignal('Value'):Connect(function()
	if currentlvl.Value >= maxlevel then
		nextlvl.Text = "Max"
		lvl.Text = "Max"
	else
		lvl.Text = currentlvl.Value
	end
end)

I made that for later use so you can see the stats of the next level that you upgrade to, but that still doesn’t fix that it upgrades twice

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)

hmm still doesn’t seem to work, in fact makes it even weirder. Now the first turret doesn’t seem to upgrade at all

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.

I have tried everything I can imagine of but I just cant seem to fix it

nvm i fixed it by instead of using the same gui, i cloned the gui and changed some scripts so it disappears with a second click