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

Why does the second while wait() in my script not run?

This is the code in one of my scripts, it's meant to call a Bindable Event every certain amount of time. But I noticed that the second wait doesn't run for some reason. And then when I delete the "wait(65)" the other wait does run.

Is this simply something I can't do? Am I not allowed to have multiple while waits in a single script?

local start = tick()

local function updateQuests(t)
	game.ReplicatedStorage.DCQuestsUpdate:Fire()
end

local function updateDailyRewards(t)
	print("DAILY REWARDS UPDATE DAILY RAN")
	game.ReplicatedStorage.DailyRewardsUpdate:Fire()
end

while wait(65) do
    updateQuests(tick()-start) 
end

while wait(10) do
	print("DAILY REWARD RAN")
    updateDailyRewards(tick()-start)
end

4 comments

0

because the first while loop never breaks, thus the code never gets to the second loop

0

I guess I should put each in their own script. Thanks

0

You don't necessarily need to put them in separate scripts, just multithread with spawn() or coroutines.

0

I would advise against using while wait() do, but, in the end, it's your choice.

1 answer

✓ Accepted answer

A while loop (or any loop tbh) won't run the code below it until it stops running. Since your loops are infinite, the first one will never let the second one run.

In order to run two such loops simultaneously, you need to use a function called spawn(). It runs the function you input in a new "thread".

spawn(function()
   while wait(1) do
        --stuff
    end
end)

while wait(2) do
    --stuff
end

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