Pathfinding issue

Hello devs, so after a long time I decided to use pathfinding for my entity in my game.


local PathfindingService = game:GetService('PathfindingService')
local Variables = {
	AgentParams = {
		AgentCanClimb = true,
		AgentRadius = 3,
		AgentCanJump = true,
		AgentHeight = 4,
		WaypointSpacing = 5,
		Costs = {
		}
	},
}

--find target just returns closest HumanoidRootPart

while task.wait() do
	local Target = FindTarget()
	
	if Target then
		local path = PathfindingService:CreatePath(Variables.AgentParams)
		path:ComputeAsync(HumanoidRootPart.Position,Target.Position)
		for i,v in pairs(path:GetWaypoints()) do
			Humanoid:MoveTo(v.Position)
		end
	end
end

Everything works except for pathfinding part.

Entity chases player properly but thing is he doesnt avoid barriers (just walking straightly to player), same as using Humanoid:MoveTo(Target.Position)

Its been like 3-4 hours since im trying to fix and my brain is melting. So I came here to ask for help more experienced devs than me.

The issue might not have to do with your script, I don’t see anything inherently wrong with it.

How thick are the barriers? if they are too thin the navmesh might miss the walls and pretend they don’t exist, causing the AI to generate a path straight to the player.

Also try using ComputeSmoothPathAsync() instead. It’s deprecated but sometimes it solves the problem.

barriers are simple, just a brick wall, agent params are setted correctly

If I will add Humanoid.MoveToFinished:Wait() script will fix BUT then entity will be moving with delays which I dont need

I see the issue now, in the for i, v in pairs loop you don’t wait for the humanoid to have moved, so it’ll cycle through all the waypoints instantly, then it’ll stop at the last waypoint which is the player position, essentially acting as a :MoveTo function.

yeah just what I replied you with, but thing is if ill add wait, then movement will be heavily delayed

That’s very strange, when waiting, does it stand still for a while on each checkpoint doing nothing?

yes if using wait, he will stand for like 0.3s (lil delay)

Just use simplepath. its literally an amazing module that doesnt get the love it deserves.

0.3 seconds isn’t much of a delay, it is pretty average for roblox pathfinding. I would suggest adding a function that makes the AI immediately move towards the target location if it has direct line of sight, and let pathfinding do it’s thing if that isn’t the case.

Alternatively, you can use SimplePath.

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