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

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

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

✓ Accepted answer

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!

2 comments

0

coroutine.wrap(function)() is much shorter than coroutine.resume(coroutine.create(function)); even shorter is delay(function,0)

0

Makes sense

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

1 comment

0

do you know the to this below?

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

if time_left==0 then
time_left = 900
end

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