Is using a while true do or using RunService better for performance?
I would assume it is while true do loop as it doesn’t fire every frame however, I could be wrong. Note: I understand the difference in each method however, strictly speaking, which one is better for performance.
A while true do loop runs repeatedly until you break it. Listening to RunService.Heartbeat runs something every frame. They are completely different, so you’ll have to explain more about what you’re doing.
This code:
local previousTime = tick()
while true do
local currentTime = tick()
model:TranslateBy(Vector3.new(0, 2 * (currentTime - previousTime), 0))
previousTime = currentTime
end
Will not smoothly animate the model. It will crash your script after about 30 seconds because you are never pausing your script for other scripts to run.
For example, if I had a bobbing animation, in terms of performance which one would be better (I understand that RunService would give a smoother result):
while task.wait() do
Part.Position = Vector3.new(0, math.sin(tick()), 0)
end