I feel like I’ve been using RunService.Stepped wrong the entire time. Now I know that there are tons of threads about when to use the events but I’m curious under my situations:
Checking distance between every NPC to your local character.
1A) RunService.Stepped for every NPC?
1B) RunService.Stepped that loops through all the NPC?
Rendering 3D Animated Models for ViewportFrames
2A) RunService.Stepped for every model?
2B) RunService.Stepped that loops through all models?
Measuring distance between waypoint and you
3A) Similar to 1, should I be a loop through all the waypoints
Custom Item Pick up system (Distance check between you and item)
4A) RunService to manage all the items
4B) RunService for each individual item
Anti exploits
5A) Runservice to check (Flying, Velocity, etc)
Checking if a player is swimming or not (In water or not custom water)
6A) Using RunService
These are the situations that I want to use some sort of loop for. My other idea would be to create an infinite loop that manages all of these each time it loops.
I’m trying to go for performance and quality (Like for 2A I don’t want the 3D models to be slow or glitchy)
EDIT: I’m still looking for answers but I’ve decided for now I’ll go with loops since it seems like it’s less likely to eat up performance
I’m just gonna say B) for all of them: having all of the instances have their own event would cause performance issues because of the amount of threads (objects that allow code to run nearly simultaneously, not an actual post) that’ll be created every physics update and would use a lot of memory
A bit pedantic but worth mentioning that Stepped runs before physics simulation, not after. Assuming by the wording of “created every physics update” that Stepped running afterward is implied, which is not the case.
I’m horrible with Lua so maybe other people can speak for how these loops work internally, but in C++ and most languages we do not have an event-based system. Usually APIs introduce events so that processes are handled internally (they still use a listener loop, but there’s less overhead). Everything is done with loops, regardless if you are using an event or otherwise.
What you use a loop for is something that gets called quite often - you don’t really need a listener, you already know what is going to happen. Checking for anything stat-based is good to use with a loop. But for the events that Roblox already supports, you should use those as they are likely to run efficiently.
The other thing to use a loop for is if you want to ignore the framerate. If something needs to be run in the background and does not depend on frames, use a loop. If it you want an accurate loop that depends on frames, use the stepped event, or heartbeat, or whatever event that satisfies this.
I do a lot of coding in C++ and since I rarely use events, it’s funny learning Roblox Lua and seeing how many events there are. And I was really weirded out that there are 3 different frame-capped loop events. It’s really confusing at times which one needs to be used, so I would suggest experimenting if you are unsure, and selecting the option that works best.