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

Why is my script not working?

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

✓ Accepted answer

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

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