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))
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.
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.
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
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!
You literally dont need it. Just set the parent/adornee of the BillboardGui whenever the distance is achieved and then activate the userinputservice func.
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 ...