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

Wierd thing , why can't i have 2 loops while true do ?

Im currently making a script but it's too long , i have 1 part that just doesn't work , there are 2 digits X and Y , X + 1 every second while Y + 1 every 5 seconds , but when i used while true do for both of these , randomly 1 gets added , the other doesn't , i don't know any other ways , can somebody give me other ways to do this ?

local x = 0
local y = 0
while true do
    x = x + 1
wait(1)
print("Test")
 end
while true do 
     y = y + 1
print("Test_2")
wait(5)
end

1 comment

0

wrap it in a coroutine or spawn(function(), they allow multiple loops to run

2 answers

✓ Accepted answer

It doesnt work bc your first loop is an infinite loop means it will never get past that loop, it will never read and execute the code below. it like the person in the comments commented if you wrap it in a coroutine it will be like you run that function from a new script. so it can continue read the next code and execute it

Just like @greatneil80 said "wrap it in a coroutine or spawn(function(), they allow multiple loops to run" you would need to either put it in a coroutine or a spawn function

local x = 0
local y = 0

local thread = coroutine.create(function()
    while true do
        x = x + 1
        wait(1)
    end
end)
coroutine.resume(thread) --starting the thread

while true do
    y = y + 1
    wait(5)
end

or

local x = 0
local y = 0

spawn(function()
    while true do
        x = x + 1
        wait(1)
    end
end)

while true do
    y = y + 1
    wait(5)
end

I don't understand what this would be used for, but I think it would be better if you use tweens or for loops.

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