NPC works fine instudio but not ingame

I have recently made a NPC that tracks you and tries to get you and it uses pathfinding and passes through doors and it works perfectly fine in studio but not in game? I assume it might be lag or something but i just don’t know how to fix it.
Here is a video to show in studio:

And in the video as you can see the NPC passes through the doors and walls quite smoothly however when we compare this to the in game version:

It is very different, firstly it is not as smooth and secondly the NPC gets stuck at the doors unlike what would actually happen in studio.
Thank you.

1 Like

Post your code. It might be getting stuck on a part. If you don’t have it scripted to reroute when stuck then it won’t move.

1 Like

Sure, but in the videos it doesn’t get stuck it just cant pass through the door.

function Pathmove(target)
	local path = game:GetService("PathfindingService"):ComputeSmoothPathAsync(SCP106HumanoidRootPart.Position, target.Position, 500)
	local waypoints = path:GetWaypoints()
	local points = path:GetPointCoordinates() 
	if path.Status == Enum.PathStatus.Success and path.Status ~= Enum.PathStatus.NoPath then
		for _, waypoint in pairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				warp(target)
			end
			local part = Instance.new("Part")
			part.Shape = 'Ball'
			part.Material = "Neon"
			part.Size = Vector3.new(0.5, 0.5, 0.5)
			part.Position = waypoint.Position
			part.Anchored = true
			part.CanCollide = false
			part.Parent = game.Workspace
			game:GetService('Debris'):AddItem(part, 10)
		end
		if #points > 2 then
			SCP106Humanoid:MoveTo(points[2])
		elseif #points > 1 then
			SCP106Humanoid:MoveTo(points[2])
		elseif #points > 0 then
			SCP106Humanoid:MoveTo(points[1])
		end
	end
end

This is the main handler for the pathfinding.

And also the doors are added to a collision group that the NPC ignores so it can pass straight through it so it shouldn’t be catching onto any parts??

This is most likely the issue. PathFindingService ignores collision groups (this was brought up in another topic here: PathfindingService treats walk-through parts set by collidable groups as non-collidable and walks around them - #14 by RecanValor), meaning that it would treat the doors in your game as collidable if its CanCollide property is set to true, despite being in a collision group that the NPC ignores. As such, PathFindingService will attempt to create a path around the door, but because this isn’t possible in your case (as the NPC starts in an enclosed area from what I can discern), :ComputeSmoothPathAsync() will return nil, although that does not seem to happen in the video (:ComputeSmoothPathAsync() in this case still seems to return a table of waypoints, but there are none outside the door). Regardless, setting the door’s CanCollide property to false should solve the problem with PathFindingService, although that would allow players to walk through the door as well.

2 Likes

Wouldn’t it be possible to change the collision group of the players so that they collide with the door and the NPC doesn’t? You would just have the NPC in the default collision group, where it cannot collide with the door, and have the players in a separate group where they do collide with the door. Similar to this article on the Developer Hub where player collisions are filtered by team. I’m not sure why it works in studio but not in game, though.

1 Like

If i change the CanCollide property wouldn’t it allow players through as well?

Changing the CanCollide property would allow players through the door as well, although as @Trickshotblaster stated, you may be able to set the collision group of the NPC to “Default”, set the door’s CanCollide property to false, create a separate collision group for players, and enable that collision group to collide with the door. This way, the door’s CanCollide property would be false, allowing the NPC to walk through it, while player characters are in a collision group that does not allow them to do so.

1 Like

It seems that the default collision group bypasseses all the other collision groups and even if i set the players collision group to a group that the door can collide with it still wont stop the player.