Which one is better RunService.heartbeat:connect or while true do and what is good alternatives to these 2?

so recently i started coding and i stumble upon question on
what is better looping code?
runservice or while true do

at one hand runservice can be used with other codes in one script but at other hand it uses frames which can be abused by increasing maximum fps

while true do or while task.wait do cannot be abused by fps or something like that but it can often crash and cannot be used with other codes in one script

right now im practicing on lua coding and i want to make player accelarate when it moves it almost working but i cannot find good looping system

sooo which one is better or what is good alternative

1 Like

RunService.Heartbeat will fire at a more consistent rate than a while loop would, or at least it does from my experience. Speaking of unlocking FPS breaking your code, utilize the deltaTime parameter to scale the acceleration speed based from it. Lower-end devices will benefit from that too - they won’t have a slowed down acceleration speed.

while task.wait() do is generally acceptable if you don’t need to do anything precise, however I personally prefer to use the Heartbeat connection so it doesn’t yield my code, nor do I have to spawn another thread.

Speaking of “being unable to use it with other codes?” in one script:
task.spawn() allows you to spawn a new thread which will not interrupt the running thread.

Read this article written by Roblox to learn some more things: Task Scheduler | Documentation - Roblox Creator Hub

2 Likes

i will try both deltatime and task.spawn()

Thanks you

1 Like

Please do note that both methods would ideally have you scale the value with the delta time! It would maximize compatibility with all frame counts.

1 Like

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