I have a function in a script that loops a blinking alarmlight, which sometimes runs for a long amount of time, causing a script timeout error. How do i prevent this, while still keeping a blinking alarmlight? The function is as follows, and is called with spawn(alarm)
.
function alarm()
local licht = workspace.Enginealarm
running = true
licht.Sound:play()
while script.Parent.Parent.Parent.Broken.Value >= 6 do
if workspace.FlickerSpecial.Alles_Killer.Disabled then
licht.BrickColor = BrickColor.new(1004)
licht.Material = Enum.Material.Neon
licht.Transparency = 0
wait(0.4)
licht.BrickColor = BrickColor.Red()
licht.Material = Enum.Material.SmoothPlastic
licht.Transparency = 0.2
wait(0.4)
end
end
licht.BrickColor = BrickColor.Red()
licht.Material = Enum.Material.SmoothPlastic
licht.Transparency = 0.2
licht.Sound:Stop()
running = false
end
2 Likes
You need to add some sort of yield in the while true loop you do.
task.wait()
should be enough. This yields the loop so it doesnt execute whats in it a trillion times without stopping (this is what causes script exhaustion errors).
If you want, you can make it so if
if workspace.FlickerSpecial.Alles_Killer.Disabled then
is not true, you do task.wait. This should have the expected behavior you want.
5 Likes
Did you understand what @savio14562 is saying?
That error message is misleading. The problem is that the script is looping too fast and consuming all the execution cycles. Its not because the script is running for a long time. When you’re ‘if’ statement is not true there is nothing for the while loop to do so it loops very quickly which is not allowed.
It is as if you are saying:
while true do
end
Now hopefully you see that can be a problem, to do nothing and repeat will consume all available processing cycles.
So if you add a wait() it will work, although I wouldn’t think this would be the best solution, it will fix the error. The script will be busy waiting, but at least it won’t fail.
Add the wait() after the ‘end’ of the ‘if’ statement. Or you could add it as an ‘else’.
Yes I do.
For some reason it went over my head that there could be an infinite loop there