Need tips for creating an interaction system of objects in my game

Hi!

I’m making an interaction system for my game where users can press a button on their keyboard to interact with a variety of objects (doors, etc) and I’m wondering about how to delegate the behaviors for interactions. Following the advice of this post: Pressing "E" to interact? Is it simple? - #3 by serverIess , I currently have a system for detecting when the player is near an interactable object, and displaying a billboard gui that tells the user to press e. Now I’m trying basically trying to fill in this part:

if Magnitude <= 10 then
   -- RUN SOME CODE
end

which governs the interactions themselves. The above statement will fire for any interactable object, and I was wondering what you would recommend to handle the task of doing different things for each object. I was thinking of storing a dictionary that maps models to their respective functions, but is there something better, or more elegant? Is there a golden standard for this or what are the options?

Thanks a ton!

I’m not quite sure what you really want but there is feature called ProximityPrompt which make this task easier for you.
To active it all you need to write simple code.

script.Parent.ProximityPrompt.Triggered:Connect(function()
     --code here
end)

But if you want to keep it the hard and old way there will be simple code to write.

local pl = workspace[game.Players.localPlayer.Name] -- or anything else
for i,v in pairs(MyFolderWithObjectToUse:GetChildren()) do
      if (pl.PrimaryPart.CFrame.p - v.CFrame.p).Magnitude <=10 then
-- code here
     end
end
3 Likes

ProximityPrompt is great, glad they added this and thanks for the tip