I am making a npc pathfinding script. The npc goes to the first destination fine, but when It heads to the second destination it only goes to one waypoint at a time I have tried changing the Wait()
at the bottom of the script but after it is finished with the destination 2 it walks about 3 studs away then stops.
code
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local path = PathfindingService:CreatePath({
AgentRadius = 3,
Costs = {Water = 10,
}
})
wait(2)
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local TEST_DESTINATION = game.Workspace.Well.Main.Position
local TEST_DESTINATION2 = game.Workspace.Table.Main.Position
local function followPath(destination)
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
-- Detect if path becomes blocked
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex >= nextWaypointIndex then
-- Stop detecting path blockage until path is re-computed
blockedConnection:Disconnect()
-- Call function to re-compute new path
followPath(destination)
end
end)
-- Detect when movement to next waypoint is complete
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
-- Initially move to second waypoint (first waypoint is path start; skip it)
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
followPath(TEST_DESTINATION)
while true do
wait(5)
if not character:FindFirstChild("Bucket") then
followPath(TEST_DESTINATION)
elseif character:FindFirstChild("Bucket") then
followPath(TEST_DESTINATION2)
end end
2 Likes
I had this same issue, I don’t exactly remember what I did to fix it but if I can remember correctly you would have to set the network owner to nil, this would be at the beginning of the script
local NPC = script.Parent local body = NPC:FindFirstChild(“Torso”) body:SetNetworkOwner(nil)
I don’t know if that is it but I’m pretty sure that is what I did, hope this helps.
1 Like
Nope, doesn’t help, but thanks for trying.
1 Like
Hmm, I don’t know what I did then to fix it, I had the same problem but also with other problems as well because I am trying to make a maze like horror game with something patrolling that maze, they would go to different points whilst looking for any players within a certain range
Follow and Target Script
local pathfindingService = game:GetService("PathfindingService")
local foundtarget = nil
local targetpos = nil
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local body = script.Parent:FindFirstChild("Torso")
local destination = game.Workspace.PizzaMen.PizzaMan1PatrolBlocks
body:SetNetworkOwner(nil)
local points = {
destination["1"],
destination["2"],
destination["3"],
destination["4"],
destination["5"],
destination["6"],
destination["7"],
destination["8"],
destination["9"],
destination["10"],
destination["11"],
destination["12"],
destination["13"],
}
foundtarget = nil
function walkRandomly()
local point = points[math.random(1,#points)]
local target = FindTarget()
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(humanoidrootpart.Position, point.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
if foundtarget == 1 then break end
humanoid.MoveToFinished:Wait()
FindTarget()
end
end
function findPath(target)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(humanoidrootpart.Position,target.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
if checkSight(target) == true then
repeat
humanoid:MoveTo(target.Position)
wait()
if target == nil then
break
elseif target.Parent == nil then
break
end
until checkSight(target) == false
foundtarget = nil
break
end
if (humanoidrootpart.Position - waypoints[1].Position).magnitude > 30 then
findPath(target)
break
end
end
end
function FindTarget()
local dist = 35
local target = nil
local potentialTargets = {}
local seeTargets = {}
for i,v in ipairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
if human and torso and v.Name ~= script.Parent.Name then
if (humanoidrootpart.Position - torso.Position).magnitude < dist and human.Health > 0 then
table.insert(potentialTargets,torso)
end
end
end
if #potentialTargets > 0 then
for i,v in ipairs(potentialTargets) do
if checkSight(v) then
table.insert(seeTargets, v)
elseif #seeTargets == 0 and (humanoidrootpart.Position - v.Position).magnitude < dist then
target = v
dist = (humanoidrootpart.Position - v.Position).magnitude
end
end
end
if #seeTargets > 0 then
dist = 35
for i,v in ipairs(seeTargets) do
if (humanoidrootpart.Position - v.Position).magnitude < dist then
target = v
dist = (humanoidrootpart.Position - v.Position).magnitude
end
end
end
return target
end
function checkSight(target)
local ray = Ray.new(humanoidrootpart.Position, (target.Position - humanoidrootpart.Position).Unit * 40)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
if hit then
if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - humanoidrootpart.Position.Y) < 3 then
foundtarget = 1
return true
end
end
return false
end
function main()
local target = FindTarget()
if target and foundtarget == 1 then
humanoid.WalkSpeed = 35
findPath(target)
else
humanoid.WalkSpeed = 22
walkRandomly()
end
end
while wait() do
main()
end
Hope you can find what you are looking for in this script