Is it better to use Coroutine or A standard loop?

Hi,

So i was wondering, is it better to use coroutine or a standard Loop?

Just a Question:

local co = coroutine.create(function()
while wait() do

  end
end)

coroutine.resume(co)

-- Or this:

while wait() do

end
3 Likes

standard loop definitely, if you need it asynchronous use spawn

5 Likes

I personally use coroutine when there are, say, several loops in a script, so I can easily share variables, however if you only need one thread, then a coroutine is redundant.

3 Likes

I was hoping to use it for an AutoSave feature with DataStoreService

2 Likes

Here’s the thing, if that’s the only thing the Script is doing, you might not need it, however, if multiple players are saving at the same time, then maybe.

2 Likes

If you’re gonna rely on a while wait loop you should probably replace wait() with task.wait() to prevent any throttling oddities.

2 Likes

I would say it depends on what you want the code to do, since coroutines aren’t actually asynchronous.

If you want code below the loop to run then you need a coroutine.

(but in any case you should do

while true do
    task.wait()
end

instead of

while wait() do

end

since wait() has throttling like Pretzelx86 said and doing while wait() do means that if wait() ever returns 0, false or nil the loop wil abruptly stop)

3 Likes

True, But I already how and why task.wait() is better, This is just a question about if I should use coroutine or a standard loop. not about wait functions or threads

Depends on what you want the code to do:

Otherwise you don’t

Mainly using it for an AutoSave feature in my game, the coroutine saves the data but below it are RemoteEvents for certain Data changes

I dont often use them so may not be a good idea

If you connect the RemoteEvents before doing the loop you don’t need to use a coroutine, otherwise you do.

Also using task.spawn() might be better…

This is my script:

-- Above here is PlayerAdded
	game.Players.PlayerRemoving:Connect(function()
		DataStore:SetAsync(Player.UserId..": Levels", Level.Value)
		DataStore:SetAsync(Player.UserId..": EXP", EXP.Value)
		DataStore:SetAsync(Player.UserId..": Credits", Credits.Value)
	end)
	
-- AutoSave
local co = coroutine.create(function()
		while true do
			task.wait(AutoSave)
			print("Auto Saving...")
			DataStore:SetAsync(Player.UserId..": Levels", Level.Value)
			DataStore:SetAsync(Player.UserId..": EXP", EXP.Value)
			DataStore:SetAsync(Player.UserId..": Credits", Credits.Value)
		end
	end)
coroutine.resume(co)
	
end)

game.ReplicatedStorage.Rebirth.OnServerEvent:Connect(function(plr)
	if plr:WaitForChild("Saved_Stats").Cash.Value >= 1000000 and plr:WaitForChild("leaderstats").Level.Value > 100 then
		warn("Rebirth Increased to 1")
		plr:WaitForChild("Saved_Stats").Cash.Value = 0
		plr:WaitForChild("leaderstats").Level.Value = 1
		plr:WaitForChild("Saved_Stats").EXP.Value = 0
		plr:WaitForChild("REXP").Value = plr:WaitForChild("leaderstats").Level.Value*250
	else
		warn("Not Enough to Rebirth..")
	end
end)

You don’t need to use coroutine because you start the loop inside of a .PlayerAdded event which already creates a new thread.

1 Like

It really depends on your specific use case and the way your code is structured, none of them is inherently better than the other
Use a coroutine if you don’t want the loop to yield the rest of the thread (such as a while-true-loop that will run at all times), otherwise don’t if that’s the intended behavior (such as a time-out system)

Thats really all i needed to know,

I didnt need a explaination on why task.wait() is better than wait()
However @GuySalami Gets the Solution for saying it first,
Thank You tho

Edit:
Yes, it was a Good Idea to use a coroutine rather than a standard loop, because without it, the RemoteEvents wouldn’t work due to the loop running forever.

1 Like

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