You can write your topic however you want, but you need to answer these questions:
**I want the npc to follow the closes person withing 50 studs while avoiding obstacles such as buildings etc
**it all works besides the fact it seems like the path dosent update it walks to were you last were then it continues to follow and eventually he stops over and over for no apperant reason
example:https://gyazo.com/6e723612b46dd5ab0003116d13132a75
What solutions have you tried so far? I am not sure how to fix the problem, I havent tried much.
tab = {}
local ps = game:GetService("PathfindingService")
hum = script.Parent:WaitForChild("Humanoid")
hr = script.Parent:WaitForChild("HumanoidRootPart")
dis = math.huge
targ = nil
regpos = hr.Position
local ps = game:GetService("PathfindingService")
game.Players.PlayerAdded:connect(function(plyr)
table.insert(tab,plyr)
end)
game.Players.PlayerRemoving:connect(function()
for i=1, #tab do
table.remove(tab,i)
end
end)
coroutine.resume(coroutine.create(function()
while wait() do
for i,v in pairs(tab) do
repeat wait() until game.Workspace:findFirstChild(v.Name)
local mag = (hr.Position - v.Character.HumanoidRootPart.Position).Magnitude
if mag<50 and mag<dis then
dis = mag
local path = ps:CreatePath()
path:ComputeAsync(hr.Position,v.Character.HumanoidRootPart.Position)
local wp = path:GetWaypoints()
targ = v.Character.HumanoidRootPart
for k,g in pairs(wp)do
hum:MoveTo(g.Position)
hum.MoveToFinished:Wait()
end
else
dis = math.huge
end
end
end
end))
tab = {}
local ps = game:GetService("PathfindingService")
hum = script.Parent:WaitForChild("Humanoid")
hr = script.Parent:WaitForChild("HumanoidRootPart")
dis = math.huge
targ = nil
regpos = hr.Position
local ps = game:GetService("PathfindingService")
coroutine.resume(coroutine.create(function()
while wait() do
for i,v in pairs(game.Players:GetChildren()) do
table.insert(tab,v)
end
end
end))
game.Players.PlayerRemoving:connect(function()
for i=1, #tab do
table.remove(tab,i)
end
end)
coroutine.resume(coroutine.create(function()
while wait() do
for i,v in pairs(tab) do
repeat wait() until game.Workspace:findFirstChild(v.Name)
local mag = (hr.Position - v.Character.HumanoidRootPart.Position).Magnitude
if mag<50 and mag<dis then
dis = mag
local ray = Ray.new(hr.Position,hr.CFrame.LookVector*5)
local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
if hit then
local path = ps:CreatePath()
path:ComputeAsync(hr.Position,v.Character.HumanoidRootPart.Position)
local wp = path:GetWaypoints()
targ = v.Character.HumanoidRootPart
for k,g in pairs(wp)do
hum:MoveTo(g.Position)
hum.MoveToFinished:Wait()
end
elseif not hit then
hum:MoveTo(v.Character.HumanoidRootPart.Position)
end
else
dis = math.huge
end
end
end
end))
This will cause the script to yield until the Humanoid reaches the destination. If you want it to continuously update the desired destination, remove this line. Keep in mind that repeatedly calculating the path will take up resources, so you might want to add some sort of time-delay to the loop.
if hit then
local path = ps:CreatePath()
path:ComputeAsync(hr.Position,v.Character.HumanoidRootPart.Position)
local wp = path:GetWaypoints()
targ = v.Character.HumanoidRootPart
if wp and wp[1] then
hum:MoveTo(wp[1].Position)
end
i changed it to this, and it didnt work. No errors.
local MIN_DISTANCE = 5
for k,g in pairs(wp) do
if (g.Position - hr.Position) > MIN_DISTANCE then
hum:MoveTo(g.Position)
break
end
end
This will cause the character to move to the closest waypoint that is at least 5 studs away. What I am guessing it is doing right now is that wp[1] is the current location.
If the character doesn’t get close enough to the other player, just change MIN_DISTANCE to a smaller number.