Why only one of my NPCs work?

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.

I don’t really know the problem but, try checking the output to check errors and always use print for debugging.

I’ve tried using print for debugging but everything seemed to work. Also I have no errors in the output.

Try checking if 2 NPCs has the same script or code.

They both have the exact same code.

Without writing up some new code for you (on mobile atm) the general issue here is that each NPC is writing their value instantaneously into your module script. The problem with that is whoever is the last one to, say, write module.human = will be the one that the module uses. The first executed NPC is most likely executing its journey through the module script but in a time frame that you cannot perceive, the other NPC is overwriting those module properties.

You can test this by adding a wait(5) or so on one of the NPC scripts. There are a few ways to accomplish solving this issue. I don’t want to say “hey do it this way or that” as it will depend on the paradigm you choose overall for your codebase that you should adhere to. If you do want some help or guidance shoot me a message and when I get to the computer I can gather you some code samples of the various ways or at least some general guides of those ways. If I was to lead you in a certain direction I would say “send .human, .torso, .dest, etc into the functions of the module”.

Wow okay I had no idea module scripts work this way. I thought that the script that requires the module will literally copy everything from that module and not overwrite it.

I tried adding wait(5) on one of the scripts and after 5 seconds the NPCs would switch.

I’d be glad if you could give me some guidance even though I know what’s the problem now I don’t know how to fix this.