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

Why is my script not working?

Asked by 10 years ago

It's meant to colour every part in the game called part. However it only colours one part even though there are lots of parts called part. Please could you fix this and make it so it colours all the parts every 30 seconds.

db = false

function onPlayerAdded()
    if db == false then
        db = true
        while true do
            for i,v in pairs (game.Workspace:GetChildren()) do
                if v:IsA("Part") then
                    if v.Name == "Part" then
                        v.BrickColor = BrickColor.new("Bright blue")
                        wait(30)
                        v.BrickColor = BrickColor.new("Bright red")
                        wait(30)
                        v.BrickColor = BrickColor.new("New Yeller")
                        wait(30)
                    end
                end
            end
        end
    end
end

game.Players.PlayerAdded:connect(onPlayerAdded)

1 answer

Log in to vote
-1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 years ago

You would generally want to use coroutines when using waits in loops, a coroutine will run its code while in the process continues the script of where it starts. For the lines of code you start the changing of colors on, use this code;

coroutine.resume( --We're running a coroutine, it has not been created yet.
coroutine.create( --Now the coroutine is created.
function() --It has to be put in a function for the coroutine to work...
                        v.BrickColor = BrickColor.new("Bright blue")
                        wait(30)
                        v.BrickColor = BrickColor.new("Bright red")
                        wait(30)
                        v.BrickColor = BrickColor.new("New Yeller")
                        wait(30)
end --End the function.
)
)--This will end the coroutine.resume

Myself, I don't quite understand coroutines, but if I know how to work them and they work the way I want them, then I'm happy. I will edit this post in a moment to find a wiki on coroutines, I don't want my Chrome for iPad to reload this page when surfing.

Here's the Beginner's Guide to Coroutines and Function Dump and Coroutine Manipulation

Ad

Answer this question