I would like to know if there’s any way to get a PathfindingLink from a PathWaypoint, because I made a basic zombie AI and I want it to open doors, so I placed a PathfindingLink inside the doors with the Label “Door” which should trigger the door being opened. There is no way to get the door though because I can’t find any function or property in the PathWaypoint that would lead me to the PathfindingLink. All I need is to be able to get the door directly from the PathWaypoint. I am aware that I could simply check for every door close to the zombie but it would be way more efficient to somehow get the PathfindingLink directly.
You should be able to use Waypoint.Label to get the Label of the PathfindingLink it crosses, if it does. Otherwise, .Label uses the name of the Material or the Label of the PathfindingModifier the PathWaypoint is inside.
This script gets every PathfindingLink in the workspace with the same Label and checks which one is the closest to the waypoint. It then returns the PathfindingLink object which allows me to get the door by using:
local door = getNearestPathfindingLink(waypoint).Parent
local function getNearestPathfindingLink(waypoint)
local closestLink = nil
local closestDistance = math.huge
-- Search for all descendants in the workspace to find PathfindingLinks with the specified label
for _, descendant in ipairs(Workspace:GetDescendants()) do
if descendant:IsA("PathfindingLink") and descendant.Label == waypoint.Label then
-- Get the two attachments of the link
local attachment0 = descendant.Attachment0
local attachment1 = descendant.Attachment1
-- Calculate the distances from the waypoint to each attachment
if attachment0 and attachment1 then
local distance0 = (attachment0.WorldPosition - waypoint.Position).Magnitude
local distance1 = (attachment1.WorldPosition - waypoint.Position).Magnitude
-- Find the closest attachment for this link
local closestAttachmentDistance = math.min(distance0, distance1)
-- Update closest link if this one is nearer
if closestAttachmentDistance < closestDistance then
closestDistance = closestAttachmentDistance
closestLink = descendant
end
end
end
end
return closestLink
end
I hope in the future, Roblox will add a :GetPathfindingLink() function that lets scripters easily get the PathfindingLink object.