I’m new to using the collection service. I decided to use it to add an “Interactive” tags to all the objects I’d like the player to be able to interact with. Specifically it’s suppose to display an “E” to indicate to the player that they need to press that key to interact.
But, when it came to having multiple doors and it needing to pop up for multiple doors, it only appears on one door.
UPDATE: Now that I’ve done some debugging I can see that theres an issue with the Adornee, I think trying to set it to a part so many times is glitching it out. It constantly only appears for ONE door out of the 4 separate doors I made. Every single test its the same door. I tested it, and its detecting the player is near the other parts, its just not setting the Adornee to that part.
Here is my UPDATED line of code:
RunService.RenderStepped:Connect(function(step)
for _, model in pairs(CollectionService:GetTagged("Interactive")) do
if player:DistanceFromCharacter(model.PrimaryPart.Position) < 10 then
updateInt(model)
else
clearInt()
end
end
end)
The updateInt() is a function that just updates the Adornee to the nearest door. The clearInt() just sets the Adornee to Nil.
Here are the functions:
function updateInt(obj)
script.Int.Adornee = obj.Proximity -- proximity is a part in the door
script.Int.Label.Visible = true
end
function clearInt()
script.Int.Adornee = nil
script.Int.Label.Visible = false
end
In studio, I’m able to manually set the Adornee to every door. It works just fine. But when its trying to set it multiple times, its not working? I even added an if statement so that it would only set the adornee if it wasn’t already. No luck
The clearInt function of yours is setting the Adornee to nil every time the distance is away from the doors. You should put that function inside the updateInt function and check if the adornee already exists.
function updateInt(obj)
if script.Int.Adornee ~= obj.Proximity then
script.Int.Adornee = obj.Proximity -- proximity is a part in the door
script.Int.Label.Visible = true
end
end
Edit: It seems like the problem is you only have one GUI for all doors. You would need a GUI for each door if u wish to display it in multiple doors.
function getDist(player, obj) -- get distance from a player and an object
local character = player.Character
if character then
local hrp = character:FindFirstChild("HumanoidRootPart")
local distance = (hrp.Position - obj.Position).magnitude
return distance
end
end
I’ve been working to debug it today, I’ve realized its an issue with the Adornee
This is my first time on the forum btw so sorry if this is stupid.
I’m not sure if I understood your problem correctly but adornees in billboard guis can be quite volatile sometimes, if you’re resetting it too much it might not display. Try making a script which will create a clone of your actual one and put it in a temp folder so it can be destroyed later, this allows you to dynamically use it too cuz you can have more than one! That’s what I did a while back.
Not sure why you can’t do it multiple times again though so sorry if this is not the solution that you are looking for.
I have this method so you don’t have to constantly call CollectionService every single frame. I find the getDist function not the best idea since it can be pushed down into one, concise line. I didn’t know why you were checking for hrp = character:FindFirstChild("HumanoidRootPart") considering you can just call for the hrp at the beginning of the script. The hrp will never disappear in the script unless an exploiter/script explicitly deletes it. But anyways, if you could try this method and tell me the results.
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local hrp = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local interactives = {}
workspace.DescendantAdded:Connect(function(child)
if CollectionService:HasTag(child, "Interactive") then
table.insert(interactives, child)
end
end)
workspace.DescendantRemoving:Connect(function(child)
if CollectionService:HasTag(child, "Interactive") then
local tablePosition = table.find(interactives, child)
if tablePosition then
table.remove(interactives, tablePosition)
end
end
end)
RunService.RenderStepped:Connect(function(step)
for _, model in pairs(interactives) do
if (hrp.Position - model.PrimaryPart.Position).Magnitude < 10 then
updateInt(model)
else
clearInt()
end
end
end)
Thank you. I didn’t quite use a folder, but instead I just copied the BillBoard Gui into the model if it didn’t have it already. Worked like a charm! Thanks so much. And thanks everyone else who contributed!