Pathfingind help?

You can write your topic however you want, but you need to answer these questions:

  1. **I want the npc to follow the closes person withing 50 studs while avoiding obstacles such as buildings etc

  2. **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

  3. 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))

1 Like

Updated code, same problem

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.

when i remove it he walks into walls lol
he dosent avoid them

This is because he will walk to the last waypoint in the list “wp”. Remove the entire for-loop and just do

if wp and wp[1] then
    hum:MoveTo(wp[1])
end

This will cause the humanoid to walk to the first waypoint in the list.

	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.

Use this code instead.

            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.

It tried to walk around the wall but got stuck on the edge, ill try adjusting the number