How to have NPC move back or teleport when stuck?

Hi guys! I’m trying to figure out how to have my npc move back or teleport away when he is stuck. It is able to understand when a wall or something is in his way, which he then will walk around the wall and continue to his destination. However occasionally it will just start running into a wall confused and gets stuck. How would I tell him to either walk back or teleport away when stuck?

Any help would be appreciated!

local npc = script.Parent
local humanoid = npc.Humanoid

local PathfindingService = game:GetService("PathfindingService")


local function canSeeTarget(target)
	local origin = npc.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, npc)
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
	
	
end


local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (npc.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			
			if distance  < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
			
		end
		
	end
	return nearestTarget
end









local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 8.5,
		["AgentRadius"] = 8.5,
		["AgentCanJump"] = false
	}
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(npc.HumanoidRootPart.Position, destination.Position)
	return path
	end


local function DeathEffect()
	print("Player Died Event Fired")
	
end


local function attack(target)
	local distance = (npc.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 4 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
		
	else
		--local attackAnim = humanoid:LoadAnimation(script.Animation)
		local playerDeath = game.ReplicatedStorage:WaitForChild("PlayerDeath")
		local player = game.Players:GetPlayerFromCharacter(target)
		--playerDeath:FireClient(player)
		target.Humanoid.Health = 0
	
		--attackAnim:Play()
		
	end
	
end






local function walkTo(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
	for index, waypoint in pairs(path:GetWaypoints()) do
		local target = findTarget()
		if target and target.Humanoid.Health > 0 then
			attack(target)
			break
			else
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end

		end
else
		humanoid:MoveTo(destination.Position - (npc.HumanoidRootPart.CFrame.LookVector * 12))
	end
	
end

local function patrol ()
	local waypoints = workspace.MazeWaypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end


while wait() do

	patrol()

	end
	--]]
1 Like

First you need a way to determine when its stuck. You could probably do this by keeping watch of the NPCs position. If it doesn’t have significant change in position value after x secs, its probably stuck. At that point, switching directions or teleport x studs in a given direction.

Any dev resources I could look at to kind of get an idea on how to implement it?

Possibly, I don’t know though. All your doing is checking the character’s position. Its up to you how to go about getting them unstuck, teleport to a start location, teleport x studs any direction, turn and move in a different direction. Depends on how you want your AI pathfinding to work.

I see that your following GnomeCode’s tutorial. Didn’t he already had a code in it that will (or will try to) get the npc to unstuck itself?

Try restart its pathfinding by checking whether its previous location is the same as its current one?

I don’t believe so, at least the videos I watched didn’t include it. I’m still quite new to programming so scripting something from scratch without any resources or something I can dissect is still quite a challenge for me haha. The problem with the npc is he will attempt to go to a waypoint and then get stuck, he won’t still be at the waypoint he previously was so I’m not sure how to check to see if he hasn’t been moving for a while.