Text Button detecting two clicks but only pressed once

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    When pressing the button it only goes through once instead of twice

  2. 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.

image

  1. 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
2 Likes

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)

1 Like

image

2 Likes

Try putting return true at the end of the if statement. That always solves the issue for me at least.

1 Like

Unfortunately it didn’t do anything


image

2 Likes

I don’t think that you put the print statement into the right position.

Relative to the loop, it should be

print("Reached this point")
-- Detect if any slot is clicked
for i, slot_frame in pairs(self._interface.MainMenu.Slots:GetChildren()) do

I have a suspicion that the loop is being reached twice, which is causing the connection to be doubled. The print statement is meant to check for this

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,

image

I got it fixed, turned out I had matching names between my events and transition in my state machine causing it to run twice when transitioning

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.