Help with E to interact system

I am trying to make an E to interact system with multiple objects to interact with. I am trying to achieve this using CollectionService and the tag editor plugin.
I tried to tag all the objects that the player can interact with and then calculate the closest interactable object so that I can make it adorned to the E billboard Gui. I have done all this in a local script.The UserInputService related code is in a different script.

The issue is that when I do CollectionService:GetTagged and print out the number of objects tagged, it prints out 0, but if the tagged objects are Models then it prints out the correct number. I have curently tagged an NPC’s UpperTorso. But it still prints out 0.
If I try this in a normal script it works perfectly even if its not a model. But I need to do it in a local script as I am calculating the distance between the player and the objects.
Here is my code so far:.

local CollectionService = game:GetService("CollectionService")
local interactable = CollectionService:GetTagged("Interactable")
print(table.getn(interactable))

local RunService = game:GetService("RunService")
local Players = game:GetService('Players')
local Ebillboard = script.Parent.Parent.Parent
local player = Players.LocalPlayer
local character = player.Character

RunService.Heartbeat:Connect(function()
   local closestDistance = 10000000
   local closestObj
   for _, object in pairs(interactable) do	
	  if (object.Position - character.HumanoidRootPart.Position).magnitude < closestDistance then
		closestDistance = (object.Position - character.HumanoidRootPart.Position).magnitude
		closestObj = object
	 end	
end
   Ebillboard.Adornee = closestObj
   print(Ebillboard.Adornee)
end)

I already tried searching on the dev forum but I couldn’t find anything related to this.
What is the issue here? Do I have to do it in a Server Script instead? Is there a better method to do this? All help is appreciated. Thank you! :slightly_smiling_face:

2 Likes

I don’t know if this would work but could you try using :DistanceFromCharacter()?

if player:DistanceFromCharacter(object.Position) < closestDistance then

I tried it but it still prints out 0 and then nil. Thanks anyways!

I can’t believe I didn’t notice this before.

local character = player.Character

Try changing that to:

local character = player.Character or player.CharacterAdded:Wait()

Sometimes the script won’t bother registering the character, I think that could be the reason why your script isn’t working.

I am currently working with a similar system with my current game. What I did was just create a folder in the workspace and used a local script to GetChildren() of that folder and used studs to measure distance. Once the player got close enough to any of the models inside that folder the icon “E” tweened in so they could interact with it.

I think that part of the script is working fine. The issue is with this part:

 local CollectionService = game:GetService("CollectionService")
 local interactable = CollectionService:GetTagged("Interactable")
 print(table.getn(interactable))

It prints out 0 here.

I will be using multiple intractable objects that do different things. Will your system work for this too?

Could you explain what you mean by intractable objects? Do you mean AI that do random acts?

You should use the # operator instead of table.getn().
7.2 – Changes in the Libraries

print(#interactable)

Does it still print 0?


This post might help you.

I mean something related to games such as bloxburg, jailbreak etc. that use E to interact systems.

Yes, it still prints out 0.
(30 chars)

1 Like

This will work in those cases but might not be the most efficient method for managing large numbers on interactive objects. I use it because all of my objects (AI Customers) perform the same function so it is easy for me to manage it in one location. How many interactive objects do you plan on having?

The game I am working on is a roleplay game, so it will be having a lot. I know that collection service could help in this but there seems to be an issue in my code related to that.

1 Like

I cannot reproduce the problem. Are you sure your objects are tagged with “Interactable”?
You can use a plugin for that or use the Command Bar.

game:GetService("CollectionService"):AddTag(Object, "Interactable")
1 Like

I did double check to make sure it is tagged “Interactable”, so yes, it is. I am already using the plugin for tagging it. The thing is, it works perfectly only if the instance is a Model in the local script, and if I try printing it in a server script, it prints correctly for all instances.

Make sure the objects are replicated to the client. If they are, for example, in ServerStorage, the client won’t be able to see them and they will be ignored, therefore, CollectionService returning an empty table.

1 Like

The objects are in the workspace though

Use Player:DistanceFromCharacter Player | Documentation - Roblox Creator Hub to determine the amount of studs you are away from said interactable object. Returns float of exact amount of studs away. Compare this with the ideal number you want, then use an upvalue to determine with UserInputService if you want your E keybind input to activate. Alternatively you could use

local DistanceFromPart = (vector1 - vector2).Magnitude

or something like that

Like I said, the part of the script related to getting the distance works fine. The issue is with the collection service part as its printing 0. Thanks though!

Do you guys know a better and efficient method to do this? Because for some reason this script is not working for me…