Image Button activated twice per activation?

As the title states, activated the image button makes it activate twice, I have multiple image buttons that are used for placing towers but since it activates twice it breaks it.

What do I do about this?
Here is my code:

local function Slots()
for _, slot in pairs(towerSlots:GetChildren()) do
if slot:IsA(“GuiButton”) then
slot.Activated:Connect(function()
print(“Slot Clicked”)
toggle = not toggle

  		if toggle and slot:GetChildren()[1] then
  			local tower = slot:GetChildren()[1]
  			
  			towerPlaceholder(tower.Name)
  			Highlight(towerToSpawn)
  			areaHighlight(towerArea)
  		else
  			removeHighlight(towerArea)
  			removePlaceholder()
  		end
  	end)
  end

end
end

My output each click:
image

This happens with each slot,.
Any help is appreciated, thanks.

try moving toggle = not toggle on top of print like this and see if it would help:

slot.Activated:Connect(function()
toggle = not toggle
print(“Slot Clicked”)

Use MouseButton1Click since the remoteevent will only be fired per click

local function Slots()
    for _, slot in pairs(towerSlots:GetChildren()) do
        if slot:IsA("ImageButton") then 
            slot.MouseButton1Click:Connect(function()
                print("Slot Clicked")
                toggle = not toggle
                
                if toggle and slot:GetChildren()[1] then
                    local tower = slot:GetChildren()[1]
                    
                    towerPlaceholder(tower.Name)
                    Highlight(towerToSpawn)
                    areaHighlight(towerArea)
                else
                    removeHighlight(towerArea)
                    removePlaceholder()
                end
            end)
        end
    end
end

that wouldn’t do anything differently

same results, clicking it once triggers it twice, it prints “Slot Clicked” twice.

Have you tried using a breakpoint to troubleshoot your issue? Place one inside and outside your callback declaration and see if only the event triggers twice.

can you show me an example? I don’t quite get what you mean.

This page details how to set up breakpoints:

You should create a breakpoint in line 2 and see how often the Slots() function gets called.
I don’t see any issue with the code you provided so I presume the underlying issue lies somewhere else.

I added a print statement right after I call the function, it printed twice:

Slots()
print(“slots are configured”)

image
I’m not really sure why this is happening.

Where inside your game is your script located?

It’s inside a screen gui in starterGui

Add a print statement in the first line in your script, see if the script itself runs twice.

have you checked that ResetOnSpawn is disabled?

it is, that is very strange. I don’t know why that’s happening

changing this does nothing. I hate how my reply has to be 30+ characters

I’ve encountered this same undesired behaviour myself. I recommend adding a check at the top of your script that checks if the Parent of your GUI is StarterGui and calls the return keyword if it is.

try doing print(debug.traceback("",0)) in the slots function and reply with the output

like this?

if mainGui.Parent == game.StarterGui then
return
end

This does nothing btw, same issues

image

You have to call return outside of any function. Alternatively you can also run script:Destroy() as far as I’m concerned.