What loop should I use for my game loop?

Hello, so when I was getting help with a problem, I was told using while true do loop is cringe and that I should use other loop, what loop is more professional?

Screenshot (322)

1 Like

while loops are not cringe, who told you that anyways?
also don’t use wait() use task.wait()

Ohh okay they might have been playing with me but whats the difference then wait() and task.wait()

you should use
while true do task.wait(time) end
cuz while wait() do is bad heres a topic: Avoiding wait() and why - Resources / Community Tutorials - DevForum | Roblox YOU SHOULD READ IT BECAUSE ITS RLLY GOOD

The reason they’re “cringe” is because they waste milliseconds even after the players have already reached 2. Using events would be a better way to make this code.

Players.PlayerAdded:Connect(function ()
if #Players:GetPlayers() >= 2 then

Read the post linked in the reply above. It’s really helpful and it changed the way I code.

oh ok I see how it can be bad.

wait but if i do this, would this reset the loop each time a player has been added?

The best way to implement a game loop is by using Heartbeat. It ensures that your code will run each frame, even when the player uses an fps unlocker.

task.wait() is a replacement for wait(). As far as I know, it should also have a 0.04 minimum value, meaning the max framerate your loop can hit is 50.

    --code here

There is no loop needed here. For the “Waiting…” text label, you could make a while loop outside this. As other people have stated, you should use task.wait() or game:GetService("RunService").Heartbeat:Wait(). They work the same, but I prefer task.wait() since it’s shorter. The reason these are better than wait() is because they are faster.

No, task.wait is tied to Heartbeat, so it would wait a maximum of 1/60 seconds, assuming max framerate.

1 Like

ok thanks ill do that instead.

what do you mean there is no loop needed for the waiting for players, I’m using that so the periods looks animated and working

Oh, I thought you were just checking if there are 2 players.

while task.wait() do or while wait() do should suffice.

1 Like

please dont recommend anything that uses wait()

Why? It hasn’t been fully deprecated yet and until it is, I’d consider it fine to use, I’m aware that it isn’t as good as task.wait() but it’s still operable.

1 Like

this is a valid point, but just because it isn’t deprecated yet doesn’t mean it’s not bad practice to use

1 Like