local NPC = script.Parent local humanoid = NPC.Humanoid local Doors = game.Workspace.DoorTarget for i, door in ipairs(Doors) do local distance = (NPC.PrimaryPart.Position - door.Positon).magnitude if distance < maxDistance then nearestDoor = door maxDistance = distance end end if nearestDoor then humanoid:MoveTo(nearestDoor.Position) end
Hi All,
I have made this script above, basically I have an NPC that needs to go to the closest "DoorTarget" block, and once done should go to the next closest "DoorTarget" and this should this continue. The max distance needs to be 20
For some reason this script doesn't want to work, please let me know if you can find this issue.
FYI: I just place this script inside my NPC
Thanks in advance all..
If you don't loop the script after it's done moving or not found a door, it won't work as you expect and also you need to wait for the move to to finish before repeating because it can do things like switching doors while moving to another door, script (edited because I missed something):
local NPC = script.Parent local humanoid = NPC.Humanoid local maxDistance = 20 local Doors = game.Workspace.DoorTarget
while task.wait() do for i, door in ipairs(Doors:GetChildren()) do local distance = (NPC.PrimaryPart.Position - door.Positon).magnitude if distance < maxDistance then nearestDoor = door maxDistance = distance end end
if nearestDoor then humanoid:MoveTo(nearestDoor.Position) humanoid.MoveToFinished:wait() end
end