I have a game with a large number of interactive objects. It’s necessary for the user to be able to interact with nearby objects by pressing a key (defaults to E). This is a common system that’s used across a lot of both Roblox and Triple-A titles. (Take Jailbreak as a popular example, when you approach a vehicle)
How have you implemented, or how would you implement this system? How would you bind functions to different objects, in a memory-efficient way? How would you find out nearby objects without a heavy impact in a user’s device?
I use CollectionService. A good method of detecting if you are nearby is using a Region3 around the character with the CollectionService:GetTagged(“Interaction”) as the Whitelist in workspace:FindPartsInRegion3WithWhitelist. You could bind this to run every 20 frames, or even on a per-second basis.
For my interactions, I always have a BillboardGui with a StringValue called “Reference” which contains the BindableEvent name to fire upon being interacted with. All my BindableEvents for interactions are in a separate folder (PlayerScripts.BindableInteractions). My interaction system will look for and fire any bindable event inside of BindableInteractions with the name given by the Reference.
It would likely be easier and more efficient to have the server store a table for BindableEvents - Object reference table in your use-case, and just have the user fire an “InteractedWith” RemoteEvent.
The server does not need to know when the client is interacting with something that may be 100% on the client. For example; an Interaction that opens a GUI, or toggles a client-sided light.
In my use case the StringValue is always the name of the desired client-sided BindableEvent to be fired. A separate RemoteEvent will be fired to the server afterwards if required.