MouseButton1Click firing more than once

for i, v in pairs(gui) do
	v.MouseButton1Click:Connect(function()
		print(v.Name)
	end)
end

when i do this, it fires the name of v 40-60 times, idk how to solve this

3 Likes

either the GUIs all have the same name or there’s duplicates inside the table gui

You never showed us what gui is so we don’t know

2 Likes

I wonder what that gui variable is. Is this a product of Instance:GetChildren() or a plain table that is continuously updated over time?

it’s a instance:GetChildren, they don’t have the same name tho

Use debounce, it helps stop multiple clicks on a button.

2 Likes

Man, why nobody talked about this? lol. Also, it’s “Debouce” not “Denounce”

You need to check whetever it is a button or not, if it is, then you must create a debounce variable, like this:

local DB = false -- you can name it whatever you want

for i, v in pairs(gui) do
if v:IsA("TextButton") then
     v.MouseButton1Click:Connect(function()
     DB = true
end
     end)
end

i made this script from zero and not from roblox studio, so it may get some bugs

maybe use :Once instead of Connect

for i, v in pairs(gui) do
	v.MouseButton1Click:Once(function()
		print(v.Name)
	end)
end

did not work, i forgot to say, the for i, v in pairs is inside a heartbeat function, so the script is like this:

game:GetService("RunService").Heartbeat:Connect(function()
	for i, v in pairs(gui) do
		v.MouseButton1Click:Connect(function()
			print(v.Name)
		end)
	end
end)
1 Like

Bro, why everyone are just ignoring me, bruh

1 Like

sorry, i forgot to say, your solution also didn’t worked

2 Likes

make debounce, like this:

local DB = {}
local DelayTime = 2 -- seconds
game:GetService("RunService").Heartbeat:Connect(function()
	for i, v in pairs(gui) do
               if not DB[v] then
               DB[v] = true
               task.delay(DelayTime ,function()
               DB[v] = nil
end)
		v.MouseButton1Click:Connect(function()
			print(v.Name)
		end)
              end
	end
end)

sorry if it has bugs i cant check
and maybe still use once instead of connect

Oh, thanks for letting me know, i try my best.

im sorry if it still doesnt work, i dont have experience with run service

Vlad, what’s the difference about once and connect??

Once automatically disconnects the function after it fires, while connect does not

ooooooo, this is super useful for some cases, i like it

You’re constantly (every frame to be exact) creating a new MouseButton1Click connection since it’s inside the Heartbeat event. Just make it standalone instead.

3 Likes

This is also a memory leak too. All these connections never get disconnected.

1 Like

And by disconnected what do you mean?