Hi folks, I'm a bit of a beginner in scripting and still trying to gather techniques to make my codes efficient. Please have a look at my script and hopefully one of you awesome helpers can tell me what I'm doing wrong, thank you!
Debounce = false function OnClicked() if Debounce == true then return end Debounce = true for i,v in pairs (script.Parent.Parent:GetChildren())do if v.Name == "LED" then v.BrickColor = BrickColor.new("Crimson") wait(2) v.BrickColor = BrickColor.new("Deep orange") wait(2) v.BrickColor = BrickColor.new("Parsley green") wait(4) v.BrickColor = BrickColor.new("Crimson") end end end script.Parent.ClickDetector.MouseClick:Connect(OnClicked)
Basically, I have 3 sets of L.E.D's I want to light up when a button is clicked, but for some reason it does work in the colour order although only 1 set of L.E.D's light up if that makes sense. Thank you all again for your time and hope to hear soon!
Something you should know is that scripts run chronologically from top to the bottom, this obviously applies to loops too. The loop is waiting for the brick to change color, then goes to the next brick. An easy fix would be to spawn a new thread for every brick.
Debounce = false function OnClicked() if Debounce == true then return end Debounce = true for i,v in pairs (script.Parent.Parent:GetChildren())do if v.Name == "LED" then spawn(function() -- makes the iterations run separately v.BrickColor = BrickColor.new("Crimson") wait(2) v.BrickColor = BrickColor.new("Deep orange") wait(2) v.BrickColor = BrickColor.new("Parsley green") wait(4) v.BrickColor = BrickColor.new("Crimson") end) end end end script.Parent.ClickDetector.MouseClick:Connect(OnClicked)
creeperhunter76's answer works, but spawning a new thread each iteration is gratuitous. You should create new threads(and use coroutines) sparingly.
I would make a function for changing the color, and simply call it multiple times.
Also, idk if this was intentional but you never turned your Debounce off.
local Debounce = false function color(c) --Function to color them for i,v in pairs (script.Parent.Parent:GetChildren()) do if v.Name == "LED" then v.BrickColor = BrickColor.new(c) end end end function OnClicked() --The variable will already return true or false --Comparing is redundant. if Debounce then return end Debounce = true color("Crimson") --Call it multiple times, with waits in between. wait(2) color("Deep orange") wait(2) color("Parsley green") wait(4) color("Crimson") Debounce = false; --Turn debounce off end script.Parent.ClickDetector.MouseClick:Connect(OnClicked)