How to make NPC reset CFrame to his spawn when NPC is stuck

Hello. I’m having a problem with NPC getting stuck so I wanted to make a script that every 15 seconds it checks if NPC is stuck or not, if NPC is stuck then NPC CFrame will be equal to his spawn, else it checks again. But it’s not working, even when NPC is chasing me it teleports him to his spawn.
Script:

local humanoid = script.Parent:WaitForChild("Humanoid")

while wait(15) do
	if humanoid.MoveDirection == Vector3.zero  then
		script.Parent.HumanoidRootPart.CFrame = game.Workspace.SpawnPatrick.CFrame
	else end
end

Any help is appreciated.

Don’t use Vector3.zero!
fifdfs

Although the npc may be stuck, it doesn’t mean that its moveDirection is zero.

Instead of checking the move direction, Try checking if the position is the same.

Actually, instead of checking if the MoveDirection is 0, you need to check if it’s not 0, as that would mean the NPC is trying to move.

My suggestion is, every X seconds (maybe something like 10 seconds) check if the NPC is trying to move and if the position is the same as the position from 10 seconds ago. If it’s the same then the NPC is most likely stuck somewhere.

I would replace Vector.zero to Vector.new(0, 0, 0)

No. Vector3.zero is a constant which doesn’t require one to create a new vector3 object

Yes I’ll just do getpropertychangedsignal(MovingDirection) if it doesn’t change code will run. Thanks!

1 Like

Yep, I though vector.zero will check if the npc is not moving anymore, but I was wrong, thanks for the help