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

Multiple Objects Won't Light Up At The Same Time?

Asked by 6 years ago

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!

0
accept my answer please creeperhunter76 554 — 6y
0
I will, it hasn't come up for me yet because I just joined. Don't worry, I will remember since you helped me out a lot! Thank you again! NovaCometT2 9 — 6y

2 answers

Log in to vote
-1
Answered by 6 years ago

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)


0
Yes! Thank you very much for your time and help, it works an absolute treat now. I will award you the answer when the button comes up due to the fact I've just joined. Thanks so much, you're awesome! NovaCometT2 9 — 6y
Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

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)
0
Thank you also for your contribution! The Debounce being left on was an accident, I had just realised it when I spam clicked and it went nuts which I wasn't expecting. Thank you again, this has also helped me! NovaCometT2 9 — 6y

Answer this question