I have a path finding script in a npc that I set to go towards a part. It will go to the part, and when I move to the part it will move to the new position. BUT If I set it to the player’s humanoid root part, it goes to where the player spawned in but not where the player is moving.
Edit: The npc now does change direction torwards the player but completes the original path first before starting a new one
New edited script:
local PathfindingService = game:GetService("PathfindingService")
local Humanoid = script.Parent.Humanoid
local Root = script.Parent:FindFirstChild("HumanoidRootPart")
--local goal = game.Workspace.Test
local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()
local function UpdatePath()
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
task.wait()
for i, waypoint in ipairs(waypoints) do
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid.MoveToFinished:Wait()
end
end
spawn(function()
while true do
wait()
UpdatePath()
end
end)
goal:GetPropertyChangedSignal("Position"):Connect(UpdatePath)
This is a temporary solution, wait() is not as efficient and decreases performance. I would recommend game:GetService("RunService").Stepped:Wait() for a more permanent solution.
But, both should work. RunService should give better performance, though.
spawn(function()
while true do
wait()
--code here
local PathfindingService = game:GetService("PathfindingService")
local Humanoid = script.Parent.Humanoid
local Root = script.Parent:FindFirstChild("HumanoidRootPart")
--local goal = game.Workspace.Test
local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()
local function UpdatePath()
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
task.wait()
for i, waypoint in ipairs(waypoints) do
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid.MoveToFinished:Wait()
end
end
while wait() do
UpdatePath()
end
goal:GetPropertyChangedSignal("Position"):Connect(UpdatePath)
end
end)
local PathfindingService = game:GetService("PathfindingService")
local Humanoid = script.Parent.Humanoid
local Root = script.Parent:FindFirstChild("HumanoidRootPart")
--local goal = game.Workspace.Test
local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()
local function UpdatePath()
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
task.wait()
for i, waypoint in ipairs(waypoints) do
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid.MoveToFinished:Wait()
end
end
spawn(function()
while true do
wait()
UpdatePath()
end
end)
goal:GetPropertyChangedSignal("Position"):Connect(UpdatePath)
Wait this is the wrong position. Let me just send you the finished product.
local PathfindingService = game:GetService("PathfindingService")
local Humanoid = script.Parent.Humanoid
local Root = script.Parent:FindFirstChild("HumanoidRootPart")
--local goal = game.Workspace.Test
local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()
local function UpdatePath()
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in ipairs(waypoints) do
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
wait(3)--Change this.
end
end
UpdatePath()