Interaction System Optimization

I want to create an interaction system. I have a central RenderStep function that iterates through every descendant of Workspace (instance:GetDescendants()) and requires any modules (found with calling instance:IsA('ModuleScript') on every descendant, would comparing instance.ClassName with a string be more resourceful?) to retrieve the maximum interaction range and hitbox of each interactable. The function then proceeds to utilize Vector3.Magnitude to determine what interactable is within range and is closest to mouse.Hit.Position (would raycasting be more resourceful?). I’m no expert, but I’m pretty confident running instance:GetDescendants() alongside multiple magnitude calculations every frame is fairly demanding. Are there any revisions I could make to lower the intensity of the script?

There’s always a better way to do something, in this case, it would be the usage of CollectionService. I’ve written a reply on a post similar to this about making an interaction system that outlines some good practices. You can find it here.

Generally speaking it’s not horrible to do do math calculations every frame, as long as you’re not doing unnecessary math calculations. For my systems, I use a combination of math calculations and raycasting to find the closest visible interaction object.

Hope my reply helped!

1 Like

Thanks for the input and apologies for the similar topic. I’ll be sure to integrate this into the script.