I wonder is this a good practice to use runService

So, I’ve heard that using a while loop with wait() inside is not good and causing lag. So, I decided to use RunService instead of using wait() with a while loop. And I come up with two solutions using RunService instead of loop.

Which is using RunService.Stepped

local time_wait_before_loop = 2
	if dt % time_wait_before_loop < 0.01 then

Or using RunService.Heartbeat

local time_wait_before_loop = 2
local total_time = 0
	total_time += dt
	if total_time > time_wait_before_loop then
		total_time = 0

I am not sure which one is better or are there is any better solution out there. If you know which of the two solutions is better, please recommend me. Or if there’s any better idea to do the while loop, please tell me in the reply. Thanks in advance!

Which solution is better?
  • RunService.Stepped
  • RunService.Heartbeat
  • Other

0 voters

Task Scheduler
Read this, it goes into mild detail with these sorts of things.

If u want really fast stuff then use heartbeat. Else you can use stepped or render stepped

I am still not sure what to choose, actually, the do_something function that I use in the example basically is the Health script in StarterCharacterScript that’s the default provided by Roblox. But, I notice in that script it uses a while loop with wait(Regen_step) which I felt might decrease my game performance. So, I planned to write my own Health script that uses RunService instead. So, I not sure which one should I use? RunService.Stepped or RunService.Heartbeat. I’ve noticed many people choosing my RunService.Heartbeat solution and I wonder why? Can someone tell me the reason to choose the heartbeat over the stepped? I felt like the heartbeat solution needs more memory to do, that it needs to use a new variable called “total_time”? While the stepped solution only uses % (Modulus) arithmetic operator calculation?

I suggest you don’t use RunService for health regen, REGEN_STEP is there to allow easy modification. (basically a compensator for if lag strikes)
Not all loops are bad, and there’s a reason why Roblox has that script there.

To answer your question on Heartbeat, it shouldn’t matter.

This is the Health script that provided by Roblox by default

-- Gradually regenerates the Humanoid's Health over time.

local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.


local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'


while true do
	while Humanoid.Health < Humanoid.MaxHealth do
		local dt = wait(REGEN_STEP)
		local dh = dt*REGEN_RATE*Humanoid.MaxHealth
		Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)

It using while loop with (local dt = wait(REGEN_STEP) which performed like wait(), and I had read many article it said that while do with wait() is not a good thing to do. So, I planned to change it to RunService instead. Or should I keep it as it be?

M pretty sure if this script is provided by roblox, it is good.

Standalone wait() to check for if something changed is bad, running something that needs a delay for its usage isn’t.
An example being a gun; most players don’t want realistic automatic weaponry, and because wait() stalls a thread for .03 seconds, it can be used as a good substitute when firing.

So, what you telling me is wait() sometimes is useful for some situation?

As is with RunService’s versions, yes. Especially with wait(n).

Ok, Thanks for clarification! So, wait(n) is still good enough if you want something to delay, especially for health regen. I understand now.