Ad
Log in to vote
0

Have a while loop run every 5 clicks? As in I want the code to run only once every 5 clicks.

Asked by 6 months ago

Well what I want to do as the Title says is have a code where every five clicks a brick will change colors. This is my code:

r = 0
script.Parent.ClickDetector.MouseClick:Connect(function()
    r = r + 1
    print (r)
end)
while wait() do
if r % 5 == 0 and r > 1 then
        script.Parent.BrickColor = BrickColor.Random()
end
end

Of course, the while loop runs as that is what it is supposed to do, and the brick surfs through colors every five clicks, instead of changing colors once. I tried using a debounce, and trying to have it not run until r = r + 5, however it takes the value of r outside of the function which would be 0 instead of the value of r currently. Any fixes? Thanks

1 answer

Log in to vote
1
Answered by
incapaz 6384 Moderation Voter
6 months ago
local clicks = 0

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    clicks = clicks + 1
    if clicks == 5 then
        script.Parent.BrickColor = BrickColor.Random()
        clicks = 0 -- Reset the clicks amount so we can do this again
    end 
end)
0
bruh Horribilis_Aedrovulf 305 — 6mo
Ad

Answer this question