OOP Pathfinding NPCs get stuck on objects

I want to make my own OOP pathfinding zombie npc system, however the zombies get stuck on stuff like trees sometimes when they’re wandering around and it’s really annoying. I’ve tried things like limiting the distance they can wander around and tried searching it up, but to no avail. Here is an video showing it:

And this is the code:

function NPC:walkRandomly(myRoot, myHuman)
	local xRand = math.random(-50,50)
	local zRand = math.random(-50,50)
	if myRoot then
		local goal = myRoot.Position + Vector3.new(xRand,0,zRand)
		local path = game:GetService("PathfindingService"):CreatePath()
		path:ComputeAsync(myRoot.Position, goal)
		local waypoints = path:GetWaypoints()

		if path.Status == Enum.PathStatus.Success then
			for _, waypoint in ipairs(waypoints) do
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					myHuman.Jump = true
				end
				myHuman:MoveTo(waypoint.Position)
				local timeOut = myHuman.MoveToFinished:Wait(1)
				if not timeOut then
					myHuman.Jump = true
					NPC:walkRandomly()
				end
			end
		else
			task.wait(1)
			NPC:walkRandomly()
		end
	end	
end
1 Like

Note the “1” here doesn’t do anything. Also, subsequent calls to the walkRandomly won’t do anything either as you supplied no arguments to it.

1 Like

For the zombies to not walk into trees and other things, you could add a PathfindingModifier and then add costs to the PathfindingModifier in your code.

Pathfinding Modifiers explanation
Roblox Docs PathfindingModifiers visual explanation ig

3 Likes

This fixed my problem, thanks.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.