While true or RunService.RenderStepped()?

Hi! I should clarify I am not using AI for my code. That was a typo on my end, thank you for bringing that up.

I use renderstepped loops for my projects; So do many others on development teams I have worked for in the past. Just because it is used commonly does not mean I used AI.

P.S also yes, I agree with the advice given. I was wondering what approach is better in general since I have seen many different methods of looping, and with what I was taught I found this renderstepped method worked fine and wasn’t as sketchy as while true

Thanks!

I will have to look at the docs a bit more, I never knew it had such a performance deficit. Thanks!!

I am primarily self taught— I learned Python as my first text based language, and Luau as my second. I am trying to improve my programming, as I know there are aspects I can improve on. Especially as I start working more with teams of people.

I appreciate all the advice and thoughts you all have given! Thank you!

1 Like

I first tried learning python too. Just about two years ago now when I started coding. Problem was python was too hard for me plus I couldn’t see progress as much as I could in roblox studio so I switched from python to luau. I still have no idea how python really works lol. I only know a bit of python fundamentals and that was it.

Heartbeat fires every frame after the physics simulation

Unless you need something by reference this works too no?

task.spawn(function(args) 
  while true do
    -- code here
  end
end, args)

Its kind of unavoidable to use this if you need multiple loops that work individually and there isn’t much of an issue as long as your code is structured enough to still be readable

Hm? You can still do multiple loops with my approach, you just avoid upvalues.

Yes that’s why I said it’s unavoidable to make multiple loops like this, generally speaking no matter if you use upvalues or not. Performance-wise it won’t matter all that much if you make multiple loops like that. At least as long the threads know when to close

Yeah I should’ve changed that all runservice events fire every frame that’s my mistake I apologize.

Yes works
You can also force localkze so it uses MOVE opcode but this option you provided is much better.

it depends largely on what you’re doing. Any runservice loop will strictly run every frame depending on which one you use (i. e. PreRender is before rendering, PreSimulation is before Physics, etc)

these should be used for code that NEEDS to run every frame before a task, such as camera movement before rendering (PreRender) or any custom physics (PreSimulation)

if it does not have to run specifically between frames and if it yields at all (you cannot task.wait() in any runservice loop for obvious reasons, you can’t wait one second while computing a frame) then use a While loop. Heavy calculations should also not be in run service loops (by heavy, i mean truly heavy like pathfinding calculations or terrain generation, not some simple vector additions) because it will bog down your framerate. while loops can run across multiple frames because it isn’t restricted by framerate, but by CPU cycles. i hope this made sense.

For example, my game has a module that will update the camera position. I use PreRender for that.

it also has pathfinding. i use a while loop for that because it can be interrupted mid computation and it’s a lot more expensive, and again, it does not need to run specifically in one frame.

(keep in mind this code snippet is not optimized at all and sucks but you can assume that running stuff like this every frame is Not Very Good for performance if done in a RunService loop since it’ll yield that frame’s execution whereas here it can be executed across several frames)

tl;dr if it’s not important or it does not need to run before a specific step in frame calculation use normal loops. if it needs to run specifically before rendering or physics then use RunService.

Keep in mind whatever code you run in a RunService loop will yield that step of the frame’s execution until your code is done, so again, expensive calculations are poopy.

i hope this made sense because it really feels like i was just blabbering and repeating myself lol.

I’m not saying anything you’re doing is wrong or AI.. I’m more making fun of AI’s constant use of frame loops, like it’s the answer to everything.

Stepped waits for top of frame, so it’s before the next frame is rendered. (before physics)
Heartbeat waits for bottom of frame, so it’s after the last frame was rendered. (after physics)
Renderstepped is like stepped but for client scripts.
task.wait(duration) is a bit generic and waits the duration without considering frame.
task.wait() waits about a 1/60th of second and resumes on the next heartbeat (end of frame)

That’s pretty much all you need to know. Depending on the case use. How you set up the loop really doesn’t matter, other than no wait at all.. instant lock up.

Too bad text can’t convey tone, some of my posts read like a jerk. I guess I need to use more emojis.
:woozy_face::roll_eyes::man_facepalming::zany_face: Clearly I’m not a writer.

local function OnStepped(TotalTime, FrameTime)
	if math.floor(TotalTime) > math.floor(TotalTime - FrameTime) then
		print("Hello world!") --runs every second
	end 
end

game.RunService.Stepped:Connect(OnStepped)

This is a fairly decent way of executing code every second. It avoids up-values while also avoiding a while true do loop.

local function OnStepped(TotalTime, FrameTime)
	local FlooredTotalTime = math.floor(TotalTime)
	if (FlooredTotalTime % 3 == 0) and FlooredTotalTime > math.floor(TotalTime - FrameTime) then
		print("Hello world!") --runs every three seconds
	end 
end

game.RunService.Stepped:Connect(OnStepped)

3 in the above snippet can be changed to executed code every n seconds.