Hey everyone.
So basically I am trying to learn pathfinding and for that I’ve created two NPCs. In my ServerScriptService I have my module script that has all the path finding logic. Each of my two NPCs have their own scripts that are exactly the same and they all inherit from the path finding module script.
The pathfinding script is very simple all it does it finds a way to a part called EndPath.
Now, I was expecting when I hit run in the studio, both of my NPCs would go after the part but for some reason, which I don’t know, only ONE of my NPCs would go after that part.
This is the path finding module script:
local module = {}
module.pathFindingService = nil
module.path = nil
module.wayPoints = {}
module.currentWayPointIndex = 0
module.human = nil
module.torso = nil
module.dest = nil
function module.FindPath(destinationObject)
module.path:ComputeAsync(module.torso.Position, destinationObject.Position)
module.wayPoints = {}
if module.path.Status == Enum.PathStatus.Success then
module.wayPoints = module.path:GetWaypoints()
module.currentWayPointIndex = 1
module.human:MoveTo(module.wayPoints[module.currentWayPointIndex].Position)
else
print(module.path.Status)
end
end
function module.OnWayPointReached(reached)
if reached and module.currentWayPointIndex < #module.wayPoints then
module.currentWayPointIndex = module.currentWayPointIndex + 1
module.human:MoveTo(module.wayPoints[module.currentWayPointIndex].Position)
end
end
function module.OnPathBlocked(blockedWayPointIndex)
if blockedWayPointIndex > module.currentWayPointIndex then
module.FindPath(module.dest)
end
end
return module
and this is the script that’s inside both of my NPCs:
local module = require(game.ServerScriptService.PathFindingModule)
script.Parent.PrimaryPart:SetNetworkOwner(nil)
module.pathFindingService = game:GetService("PathfindingService")
module.human = script.Parent:WaitForChild("Humanoid")
module.torso = script.Parent:WaitForChild("UpperTorso")
module.dest = workspace:WaitForChild("EndPath")
module.wayPoints = {}
module.currentWayPointIndex = 0
module.path = module.pathFindingService:CreatePath()
module.path.Blocked:Connect(module.OnPathBlocked)
module.human.MoveToFinished:Connect(module.OnWayPointReached)
module.FindPath(module.dest)
In this screenshot you can see only one of the NPCs work:
(It was supposed to be a video but I had trouble uploading it)
I really don’t know what’s wrong and what to do so I’d be really glad if someone could help and explain this to me.
Thanks.
