Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to detect if a button from a gui has been clicked?

I had this question for a while now, I went ahead and made a working script that does the job that I want it to do, but the problem is that it is in a loop. So everytime you press one of the buttons, the game lags for a bit, and it does what I want it to more than once, so its very annoying. I was wondering if anyone knew any other ways. Heres what I pretty much used:

while wait() do
for i, v in pairs(youdontneedtoknowthislol:GetChildren()) do
 if v:IsA("ImageButton") then
v.MouseButton1Click:connect(function()
--the thing i want it to do 
end)	
end
end 
end 

thanks a lot! I appreciate any help.

1 comment

0

Yes, I have tried putting a small wait time on there, but it still loops a ton and lags.

1 answer

✓ Accepted answer

You don't need the loop at all. MouseButton1Click is an event that fires every time the button is clicked regardless if it's in a loop. Here's the code you should have:

for i, v in pairs(youdontneedtoknowthislol:GetChildren()) do
	 if v:IsA("ImageButton") then
		v.MouseButton1Click:Connect(function()
			--the thing i want it to do 
		end)    
	end
end 

1 comment

0

never thought of it like that! Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.