*Someone told me that you can use coroutines in order to prevent a glitch from happening. I tried it, and it still glitches.
I ran the server. The moving part successfully moved until five seconds. It blinked blue and orange.
This is the "before" of the script.*
while true do while true do script.Parent.CFrame=script.Parent.CFrame+Vector3.new(0,0,0.5) wait() script.Parent.BrickColor=BrickColor.new("Bright orange") end wait(5) while true do script.Parent.CFrame=script.Parent.CFrame+Vector3.new(0,0,-0.5) wait() script.Parent.BrickColor=BrickColor.new("Bright orange") end wait(5) end
This is the "after" of the script when I converted the loops into coroutines.
local Loop1 = coroutine.create(function() while true do script.Parent.CFrame=script.Parent.CFrame+Vector3.new(0,0,0.5) script.Parent.BrickColor=BrickColor.new("Bright blue") wait() end end) local Loop2 = coroutine.create(function() while true do script.Parent.CFrame=script.Parent.CFrame-Vector3.new(0,0,0.5) script.Parent.BrickColor=BrickColor.new("Bright orange") wait() end end) while true do coroutine.resume(Loop1) coroutine.yield (Loop2) wait(5) coroutine.resume(Loop2) coroutine.yield (Loop1) wait(5) end
What's wrong with the "after" code, and what can I do to fix the "before" code?
I would not suggest using coroutines
for your script because they are unnecessary for what you are trying to do. I'm assuming that what you're trying to do is make each while
loop run for 5 seconds, and then continue on to the next while
loop. If that's true, then right away there are problems
First of all, you're using an infinite loop that will never break. If you want the loop to run for only a specific amount of time, I would suggest using either a for
loop or using the tick()
command. For our sake, we're going to be using the tick()
method.
tick()
is a command that will return the number of seconds that have passed since January 1st, 1970. It's that way because that's UNIX time (More info on UNIX time: UNIX).
We can use the tick()
command to get the amount of time elapsed between 2 points in a script, or for many other applications. For our purpose, we're going to use it to tell when 5 seconds has elapsed since the start of the while
loop.
In order to do that, we first have to define what the current time is before the loop starts, so we have an "origin" time, like so:
local Start = tick() --The variable "start" now contains the amount of seconds that have passed since January 1st, 1970
Now the second part is to check how much time has elapsed since the start, and in order to do that, we call the command a second time and subtract the 2 times to get how much time has passed in between them, like so:
local Start = tick() while true do local Elapsed = tick() - Start --The variable "Elapsed" now contains the amount of seconds that have passed since the Start script.Parent.CFrame=script.Parent.CFrame+Vector3.new(0,0,0.5) script.Parent.BrickColor=BrickColor.new("Bright blue") wait() end
Now that we can get how much time the loop has been running for, we need a way to break the loop after it's been running for 5 seconds. We use the break
command for that. The break
command is used to exit any running loop and continue with the thread. We can use it like so:
local Start = tick() while true do local Elapsed = tick() - Start if Elapsed >= 5 then --If the elapsed time is greater than or equal to 5 seconds... break --break out of the loop and continue with the thread end script.Parent.CFrame=script.Parent.CFrame+Vector3.new(0,0,0.5) script.Parent.BrickColor=BrickColor.new("Bright blue") wait() end
Now that you know how to make the first while loop run for 5 seconds, you can use the same method for the rest of your loops. Hope this helped!