Help with E to interact system

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…

And you need CollectionService why exactly?

You literally dont need it. Just set the parent/adornee of the BillboardGui whenever the distance is achieved and then activate the userinputservice func.

But dont I need to have a table of all the intractable objects so that I can set the adornee of the billboard gui to the closest one?

If your designing a generic script and not an individual one per object then make a folder in serverstorage or something containing bool or string values. Set the name or value to the interactable objects path or name. I’d recommed using boolvalues as its simpler.

You can do this all in a script.

local InteractableObjs = {"list em here"}
local Controller = instance.new("Folder") --remember second parameter of instance causes a delay!
Controller.Parent = game:GetService("ServerStorage")

local closest

for i,v in pairs(InteractableObjs) do
    local val = instance.new("BoolValue")
    val.Name = v
    val.Value = false 
end

RunService.Heartbeat:Connect(function()
    -- distancing stuff here
    if distance < num then
        Controller["Closest Obj"].Value = true
        closest = tostring(Controller["Closest Obj"])
    else
         return
    end
    Gui.Adornee = closest
end)

UserInputService.InputBegan ...