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

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

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

✓ Accepted answer
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)

1 comment

0

bruh

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