NPCs keep running into each other

Since onWaypointReached() is connected to humanoid.MoveToFinished, it should print the debug message “Reached…” when the NPC reaches the first waypoint and then move the NPC to the next waypoint. It looks like the NPC isn’t reaching the first waypoint.

Actually, I got an error for

saying that reached was a boolean and I was trying to print it as a string (or something)

Sorry, this is turning into a discussion!

There’s nothing to apologize for. The basic premise is still there; you have a problem with pathfinding and we’re trying to help you with it.

So then it looks like the NPC did reach the first waypoint. Before going on, is there anything else we should know or are you only seeing “Success” and “Reached…” in the output? What about after you fixed the boolean error?

If you don’t know how to fix the boolean error, there are a couple ways to solve it.

print("Reached: " .. tostring(reached))
-- or
print("Reached:", reached)

I see this

Success
Error

then a whole other random stuff.

You’re going to have to be a little more specific. As I understand it, you see those two things and nothing else is relevant to the pathfinding issue. The NPC is walking in place and… did it or did it not print “Reached…”? Also, how come it printed “Error” and what does that mean? I don’t see a debug message in your code printing “Error”. Or are you saying that boolean-string-concatenation error is still there and it’s printing that error?

Error was just meant for the error I was talking about (the boolean one). I didn’t exactly remember it.

Aha I found the error!


With this line:

print("Reached: " .. tostring(reached) .. "Waypoint Index: " .. currentWaypointIndex)

It ended up saying true: it reached the waypoint (first pic). Then on the way back (first/second pic) the NPC stopped like it always did and it said false instead of true.

So with this function

local function onWaypointReached(reached)
	print("Reached: " .. tostring(reached) .. " Waypoint Index: " .. currentWaypointIndex)
	if reached and currentWaypointIndex < #waypoints then
		currentWaypointIndex = currentWaypointIndex + 1
		humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	elseif reached and currentWaypointIndex == #waypoints then
		if not isOnReturnPath then
			wait(1)
			isOnReturnPath = true
			followPath(returnDestination)
		elseif isOnReturnPath then
			wait(1)
			isOnReturnPath = false
			followPath(destination)
		end
	end
end

I need to find somewhere to put if reached is false.

It could have failed to reach the waypoint if the path was blocked, however, if the path was blocked, then it should have computed a new path to the destination, in which case the problem would be automatically handled. But if you didn’t see any errors about not being able to compute a new path and the NPC stopped moving, that means it failed to fire the Blocked event when the NPC couldn’t reach the next waypoint.
Calling followPath() again if the waypoint was not reached could be one thing you could try.

1 Like

Yay I found my solution! When reached was false, I fired the followPath() function again and it worked!

Wait, I still have a problem. Changing the script didn’t help me with my original problem. The NPCs still run into each other…

Using Physics Service is the easist and best way to do that.

How would I do this? Just note that I don’t really want the NPCs to be set to CanCollide = false.

Just do what it says on the physics service page I linked. In other words, like this.

-- the physics service
PS = game:GetService("Physics Service")
-- makes the collison group
PS:CreateCollisionGroup("NPCs")
--[[ sets the collide value of collision group "NPCs" to false, so all parts under the collision group 
"NPCS" do not collide with each other]]
PS:CollisionGroupSetCollidable("NPCs","NPCs", false) -- group1, group2, cancollide
-- example code as to how to set a parts collision group:
-- game.Workspace["something here"]:SetPartCollisionGroup("NPCs")

now just set every part inside your NPC to the collision group “NPCs” using the example code I gave you.

1 Like

Do I have to do anything else? This is what I just added:

local PhysicsService = game:GetService("PhysicsService")

-- Physics functions
PhysicsService:CreateCollisionGroup("NPC")

PhysicsService:CollisionGroupSetCollidable("NPC","NPC",false) 

He’s not asking you to set canCollide to false, he suggested making a collision group out of all the rigs. This way the rigs can’t collide with each other, but they can collide with anything else.

I’ll try and see what I can do.

local PhysicsService = game:GetService("PhysicsService")

-- Physics functions
PhysicsService:CreateCollisionGroup("NPC")

PhysicsService:CollisionGroupSetCollidable("NPC","NPC",false) 

for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		PhysicsService:SetPartCollisionGroup(v, "NPC")
	end
end

Is this good? Or do I still have to add more?

2 Likes

Sorry for the late reply, and yes, your good :smiley: try it out and see if it works!

I think the npc stopped moving again after that.

Sorry, could you elaborate? What box are you talking about?