You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
When pressing the button it only goes through once instead of twice
What is the issue? Include screenshots / videos if possible!
Whenever I press the button it goes through twice causing events to be fired twice and I don’t want to overload the server/make teleports fail from it.
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I’ve tried connections (as you can see in the code) but it didn’t work, I’ve tried debounces it didn’t work either
-- Detect if any slot is clicked
for i, slot_frame in pairs(self._interface.MainMenu.Slots:GetChildren()) do
if slot_frame:IsA("TextButton") then
Connections[slot_frame.Name] = slot_frame.MouseButton1Click:Connect(function()
print(slot_frame.Name)
Connections[slot_frame.Name]:Disconnect()
-- If clicked then request the server to set that as the slot
silk.Packages.Network:GetCommunicator({"EntityComm", "RequestSlot"}):FireServer(slot_frame.Name)
self._interface.MainMenu.Slots.Visible = false
silk.Packages.Network:GetCommunicator({"EntityComm", "RequestTeleport"}):FireServer("MainGame")
end)
end
Can you please put a print statement just prior to the for loop, and let us know how many times the this new print statement fires?
(Also, with your connections thing - it is easier if you just use :Once in place of :Connect, as it will automatically disconnect it after a single activation)
Yes, I think we need to know or see more of where this script is setup.
My suspicion is that the whole block of code that you did show is being ran/called twice.
So it did turn out that the function that the loop is in was running twice but where i activate the function; it’s not ran twice
on_mainmenu = function(state, event, from, to, msg)
self._interface.MainMenu.Visible = true
self._interface.MainMenu.Buttons.Visible = true
print(2)
self._interface.MainMenu.Buttons.PlayButton.MouseButton1Click:Connect(function()
self._interface.MainMenu.Buttons.Visible = false
print(1)
state.slots()
end)
for i, v in pairs(INTERFACE_DATA.MainMenu.slot_cache) do
local SlotTemplate = self._interface.MainMenu.Slots.Template:Clone()
SlotTemplate.Name = i
SlotTemplate.Parent = self._interface.MainMenu.Slots
SlotTemplate.Visible = true
-- Set up slot frame
SlotTemplate.Slot.Text = tostring(i .. ":")
SlotTemplate.SlotName.Text = v.Name
end
end,
on_slots = function(state, event, from, to, msg)
self._interface.MainMenu.Slots.Visible = true
print("ran slots")
-- Detect if any slot is clicked
for i, slot_frame in pairs(self._interface.MainMenu.Slots:GetChildren()) do
if slot_frame:IsA("TextButton") and slot_frame.Name ~= "Template" then
Connections[slot_frame.Name] = slot_frame.MouseButton1Click:Once(function()
print(slot_frame.Name)
Connections[slot_frame.Name]:Disconnect()
-- If clicked then request the server to set that as the slot
silk.Packages.Network:GetCommunicator({"EntityComm", "RequestSlot"}):FireServer(slot_frame.Name)
self._interface.MainMenu.Slots.Visible = false
silk.Packages.Network:GetCommunicator({"EntityComm", "RequestTeleport"}):FireServer("MainGame")
end)
return true
end
end
end,