I’m having problems with my script where the NPCs don’t walk, they spawn randomly at the points as seen at the end of the code, however, they stay stacked and don’t move. There is nothing in the output.
When I use it without Pathfind they move, but slowly. I had to use Pathfind to prevent the NPCs from getting stuck in the walls.
Here is the code:
--NPCs System
local npcPoints = workspace.NPCWalkSystem:GetChildren()
local pathFind = game:GetService("PathfindingService")
local npcs = script.Model:GetChildren()
for _,v in npcPoints do
v.Transparency = 1
v.CanCollide = false
end
function getNearPoint(npc, exclude)
local closest = {nil,math.huge}--Part, distance
local searchPoints = {}
for _, v in npcPoints do
if v ~= exclude then
table.insert(searchPoints, v)
end
end
for _,v in searchPoints do
local disance = (v.Position - npc.HumanoidRootPart.Position).magnitude
if disance < closest[2] then
closest = {v,disance}
end
end
return closest[1]
end
local function walk(npc, lastPoint)
local point = getNearPoint(npc, lastPoint)
local lastPoint = point
local path = pathFind:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, point.Position)
local waypoints = path:GetWaypoints()
for _,v in waypoints do
npc.Humanoid:MoveTo(v.Position)
npc.Humanoid.MoveToFinished:Wait()
end
spawn(function()
walk(npc, lastPoint)
end)
end
for _, v in npcs do
v.Parent = workspace
v:MoveTo(npcPoints[math.random(1, #npcPoints)].Position)
--spawn a async function
spawn(function()
walk(v, nil)
end)
end