How would I make a wait() in a while loop?

I am currently developing a program that awards points to players and grants them cash after a 5-second interval. However, the program currently has a flaw wherein points and cash are being awarded simultaneously, rather than one after the other. I am seeking a solution to address this issue and ensure that points and cash are granted separately, as intended.

Here’s the code!

while Started.Value == true do

	Time.Value += 1

	if Time.Value > BestTime.Value then
		BestTime.Value = Time.Value
	end
	
	wait(5) do
		
		Folder2.Token.Value += 1
	end
end

You could just wait a few seconds before restarting code in the while loop:

while task.wait(5) do -- Waits 5 seconds before restarting code inside while loop
	Time.Value += 1 -- Adds 1 time every 5 seconds
end
1 Like

I apologize, what I’m looking for a code where no matter what is waiting (I.e. Token), it’ll give the seconds instead of waiting a additional 5 seconds just to get +1 time.

Could you elaborate more please I’m am a little confused.

1 Like

Okay, If a Player started a round. It’ll start giving time. But, the time will give once, and wait 5 seconds to give a Token until it repeats.

Ah ok. This may do the trick:

while Started.Value == true do
	Time.Value += 1

	if Time.Value > BestTime.Value then
		BestTime.Value = Time.Value
	end
	
	task.wait(5)
		
	Folder2.Token.Value += 1
end
1 Like

Thanks, but unfortunately. It didn’t work.

First, It’ll give time once, wait 5 seconds, give token, and repeat. (There’s a pause where it doesn’t give the time once, (Like every 1 second it’ll give time).

So every one second you want it to give you + 1 time?

1 Like

I am not sure what you want but this might be it

while Started.Value == true and task.wait(1) do

	Time.Value += 1

	if Time.Value > BestTime.Value then
		BestTime.Value = Time.Value
	end
	
	delaly(5, function()
         Folder2.Token.Value += 1
    end
end
4 Likes

Not quite sure if this is what you’re asking for, but here:

while Started.Value == true and task.wait(1) do
	Time.Value += 1

	if Time.Value > BestTime.Value then
		BestTime.Value = Time.Value
	end
	
	if Time.Value % 5 == 0 then
		Folder2.Token.Value += 1
    end
end
1 Like

It’s best to not use wait() at all honestly. Also, wait() is deprecated, the correct statement is task.wait().

See this post here for more information:

1 Like

I could be totally wrong here with interpreting your question, but I want to offer this to hopefully nudge you in the right direction.

In fact, @kittyPGR has already done the same thing. But it sounds like you want your main while loop to continue running even though you are still waiting for 5 seconds. You don’t want the waiting to halt the while loop.

For that, you would need to create a new coroutine to run that code seperately from the while loop.
Like:

task.delay(5, function()
– Stuff to do after 5 seconds
end)

But I would refer to @kittyPGR 's code for what your code should look like. They also added in a “wait” for one second as well. So keep that in mind.

Also, if you don’t want to your “Token += 1” code to always happen after 1 second, you would also need to put it within another if statement, but I don’t know if the Time.Value > BestTime.Value is where you would want to put that. But I’m just saying in the event another issue arrises on your end.

1 Like

The issue is you increment time, wait 5 seconds, increment token and then immediately go back to time again because the loop ends. Just add another wait(5) after Folder2.Token.Value += 1

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.