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

Is there anyway to use a loop but keep other functions going that are after it?

Asked by
wjs3456 90
10 years ago

So I have this script:

function countdown()
    time_left = 900
    while true do   
        time_left = time_left - 1
        wait(1) 
    end 
end

It works but the problem is is that I want it let the other functions after it to also go. Is there any way to place the while true do somewhere else or is there another loop type

Thanks

3 answers

Log in to vote
1
Answered by 10 years ago

Ya, I had this same kind of problem, try using Coroutine's, this type of Coroutine I'm about to show you, it can do multiple codes at once;

coroutine.resume(coroutine.create(function()
while true do
wait(5)
print("I'm in a coroutine!")
end
end))

coroutine.resume(coroutine.create(function()
while true do
wait(5)
print("I'm in a coroutine too!")
end
end))

while true do
wait(5)
print("I'm not!")
end

As you'll see, every 5 seconds, the three loops will print I'm in a coroutine!, I'm in a coroutine too!, I'm not! in the Output. The coroutine.resume(coroutine.create(function() end)) allows you to do multiple loops/codes at once. Head to this link for more information about Coroutine's, http://wiki.roblox.com/index.php?title=Coroutines. Hope this helped!

0
`coroutine.wrap(function)()` is much shorter than `coroutine.resume(coroutine.create(function))`; even shorter is `delay(function,0)` BlueTaslem 18071 — 10y
0
Makes sense wjs3456 90 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

Using a while true do blocks all functions after it. I suggest using a more simple countdown like a loop for example.

local time = 900
function countdown()
for i = 1,900 do
wait(1)
time = time-1
end
0
do you know the to this below? wjs3456 90 — 10y
Log in to vote
0
Answered by
wjs3456 90
10 years ago

Is there any way to make it so you could do something like this?

if time_left==0 then
time_left = 900
end

Answer this question