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

How do I make two "for" "do" loops work at the same time?

In a "for" "do" loop, have can you have more than one set of rules happening at once, for instance having:

'i' decreasing to 0, whilst 'a' increases to 2.

I would have imagined it would look like this, but it does not seem to work:

for i=1, 0, -0.1 do
	for a=1, 0, 0.1 do
		wait(0.5)
		print(i)
		print(a)
	end
end

This is obviously just a scenario, however how do I get them both to count simultaneously on the output?

1 comment

0

You created a nested for loop, which is very useful for other things later.

2 answers

✓ Accepted answer

I don't know if this would be the most efficient way of doing this, but you could use coroutines, and it would look something like this:

coroutine.resume(coroutine.create(function() for i = 1,10 do print(i) wait() end end))
coroutine.resume(coroutine.create(function() for i = 1,10 do print("I'm printing!") wait() end end))

1 comment

0

Coroutines are more effective than the spawn function in this case, because they are executed without delay.

Two different scripts or two different threads; First one is pretty self explanatory so I won't explain that method.

You can create a new thread with the inbuilt spawn function, it takes a function parameter

spawn(function()
	while true do
		wait(1)
		print'Im looping :D'
	end
end)

print'i printed even with a loop above me'

for your case, something like this

spawn(function()
for i = 1, 10 do
print(i)
end
end)

for i = 1, 20, 2 do
print(i)
end

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