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:
This happens with each slot,.
Any help is appreciated, thanks.
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
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.
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’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.