Script error: Script timeout: exhausted allowed execution time

how do I fix this and what does it mean?

here is my code:

local darkLeaves = game.Workspace.Trees.DarkLeaves


while true do
	for i , colorPart in ipairs(darkLeaves:GetChildren()) do
		colorPart.BrickColor = BrickColor.new("Ghost grey")
	end
end

wait(20)

while true do
	for i , colorPart in ipairs(darkLeaves:GetChildren()) do
		colorPart.BrickColor = BrickColor.new("Parsley green")
	end
end

how do I fix this?

in all while true loops you need to add a task.wait() somewhere

local darkLeaves = game.Workspace.Trees.DarkLeaves


while true do
	for i , colorPart in ipairs(darkLeaves:GetChildren()) do
		colorPart.BrickColor = BrickColor.new("Ghost grey")
	end
task.wait()
end

wait(20)

while true do
	for i , colorPart in ipairs(darkLeaves:GetChildren()) do
		colorPart.BrickColor = BrickColor.new("Parsley green")
	end
task.wait()
end
1 Like

I added a task.wait to the script and now there are no more errors but the script still doesn’t to what its supposed to.

add wait() to the start of while true do below it.

that didnt work either, this is what I have right now.

local lightGrass = game.Workspace.Terrarin.Lighter



while true do
    
    for i , colorPart in ipairs(lightGrass:GetChildren()) do
        colorPart.BrickColor = BrickColor.new("White")
        task.wait()
    end
end

wait(20)

    while true do
    task.wait()
    for i , colorPart in ipairs(lightGrass:GetChildren()) do
        colorPart.BrickColor = BrickColor.new("Sea green")
        task.wait()
    end
end
 

A while loop yields entire scripts while it is running unless it is broken (break) hence why your second loop isn’t running.

You have to put those while loops in threads

local darkLeaves = game.Workspace.Trees.DarkLeaves

task.spawn(function()
while true do
	for i , colorPart in ipairs(darkLeaves:GetChildren()) do
		colorPart.BrickColor = BrickColor.new("Ghost grey")
	end
task.wait()
end
end)

wait(20)

--task.spawn(function()
while true do
	for i , colorPart in ipairs(darkLeaves:GetChildren()) do
		colorPart.BrickColor = BrickColor.new("Parsley green")
	end
task.wait()
end
--end)

--Remove the comments if you're going to have any code down here

While loops yield the code under it and focuses on the code inside of it. But if you make new threads (basically a script in a script) using task.spawn(), the code under can run perfectly fine.

In order to prevent said yielding you can use coroutine.wrap or just combine both loops.

while true do
    for i , colorPart in ipairs(lightGrass:GetChildren()) do
        colorPart.BrickColor = BrickColor.new("White")
    end
        task.wait(20)
     for i , colorPart in ipairs(lightGrass:GetChildren()) do
         colorPart.BrickColor = BrickColor.new("Sea green")
    end
    -- task.wait(20) ?
end

2 Likes

Why do you need the while loops anyway?

local lightGrass = game.Workspace.Terrarin.Lighter

for i, colorPart in ipairs(lightGrass:GetChildren()) do
    colorPart.BrickColor = BrickColor.new("White")
    task.wait()
end

task.wait(20)

for i, colorPart in ipairs(lightGrass:GetChildren()) do
    colorPart.BrickColor = BrickColor.new("Sea green")
    task.wait()
end
1 Like