button.Activated function firing multiple times

So on the client I have a script that has a function and then inside of that function has another function. It doesn’t seem to matter if I have the function inside the other or not, but for some reason when a button I have is clicked, the function fires multiple times. Here’s the small section of script where that happens:

		newButton.Activated:Connect(function()
			
			buyButton.Activated:Connect(function()
				interactItem(tower.Name)
				print("Interacted")
			end)

So when the newButton is activated, then the buyButton can be activated, but for some reason whenever I go into the server to test, this happens:

I would attach a video but it won’t let me. So as you can kind of see, I have a line that prints out when the function takes place and the function happens multiple times and increases as I use the function. But, when I use the function the first time, it only calls once, so the function doesn’t actually get messed up at all.

Here’s the InteractItem function:

local function interactItem(itemName)
	local data = interactItemFunc:InvokeServer(itemName)
	if data	then
		playerData = data
		updateItems()
	end
end

I’m not sure if this is a common issue, or if there’s any way to fix it without seeing a lot of my script, but if there isn’t I can show more of my script.

1 Like

Thats because your establishing the listener each time.
try doing

buyButton.Activated:Connect(function()
				print("Interacted")
			end)
end)
buyButton.Activated:Connect(function()
				print("Interacted")
			end)
end)

You will see “interacted” printed twice.

Heres a hacky workaround. Let me know if it doesn’t work. :slight_smile:

local CanShop = false
newButton.Activated:Connect(function()
 CanShop = not CanShop
end)

buyButton.Activated:Connect(function()
 if CanShop == true then
  interactItem(tower.Name)
  print("Interacted")
 end
end)
2 Likes

Still not working unfortunately the same issue is still happening

What does the console say, Is it the exact same?

how about using booleans instead of triggering the event every time you activate the tool?

Yeah, everytime I do the function it increases by 1

Its possible you have a third listener above the two. Can I see the first script?

If the only purpose of newButton is to reveal or unlock the buyButton, then you can use a boolean variable instead of a nested function to unlock the buyButton via the newButton clicking.

Here’s what the code could look like:

local buyButtonCheck = false

newButton.Activated:Connect(function()
    buyButtonCheck = true
end)

buyButton.Activated:Connect(function()
    if buyButtonCheck then
        interactItem(tower.Name)
        print("Interacted")
    end
end)

Sorry for the late response, but I’m not exactly sure what you’re asking, so I’m gonna explain it a little bit better than before so it’s easier to understand what’s happening

A have a function updateItems() which is used and has a for i, loop in it. Inside of that for i, loop, there is a newButton.Activated:Connect(function() with the buyButton.Activated:Connect(function() inside of it. The buyButton.Activated:Connect(function() calls another function called interactItem(tower.Name). In a serverscript I have a line that calls the interactItem function which is:

ReplicatedStorage.InteractItem.OnServerInvoke = function(player, itemName)

Hope this helps you understand better. The InteractItem function is just basically what happens when the button is interacted with.