RunService.Heartbeat:Connect(function()
for objectIndex, object in workspace:GetDescendants() do
if object.Name:match("Interactable") then
if object:GetAttribute("Pressed") == true then continue end
local findHitbox = object:FindFirstChild("Hitbox")
if not findHitbox then continue end
local distance = (findHitbox.Position - Character.HumanoidRootPart.Position).Magnitude
if distance <= 7 then
interactableNear = object
Shared.Guis.HUD.InteractLabel.Visible = true
Shared.Guis.HUD.InteractLabel.Text = "[E] To "..object:GetAttribute("InteractName")
elseif distance > 7 and interactableNear == object then
Shared.Guis.HUD.InteractLabel.Visible = false
interactableNear = nil
end
if interactableNear ~= nil then
if (Character.HumanoidRootPart.Position - findHitbox.Position).magnitude < (interactableNear.Hitbox.Position - Character.HumanoidRootPart.Position).magnitude then
interactableNear = object
Shared.Guis.HUD.InteractLabel.Visible = true
Shared.Guis.HUD.InteractLabel.Text = "[E] To "..object:GetAttribute("InteractName")
end
end
end
end
end)
This is my code for nearest interactable. But if there are lets say 2 interactable’s like close to each other. It kind of bugs out. I want it to choose the NEAREST to the player. and also they are able to use the interactable behind a wall.