Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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.

0
Yes, I have tried putting a small wait time on there, but it still loops a ton and lags. shootthegame 14 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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 
0
never thought of it like that! Thank you so much! shootthegame 14 — 6y
Ad

Answer this question