NPC Path-finding not working correctly, and is very chunky!

What I want to achieve is a successful pathfinding for an NPC, that could not be pushed around, and is smooth.

I have tried going on wiki, but it is very glitchy, forexample, the NPC could be pushed, and it’s walking is very chunky/not smooth, and breaks most of the time.

Script: `local PathfindingService = game:GetService(“PathfindingService”)

– Variables for the zombie, its humanoid, and destination
local zombie = game.Workspace.Characters.Laila
local humanoid = zombie.Humanoid
local destination = script.Block

– Create the path object
local path = PathfindingService:CreatePath()

– Variables to store waypoints table and zombie’s current waypoint
local waypoints
local currentWaypointIndex

local function followPath(destinationObject)
– Compute and check the path
path:ComputeAsync(zombie.Head.Position, destinationObject.Position)
– Empty waypoints table after each new path computation
waypoints = {}

if path.Status == Enum.PathStatus.Success then
	-- Get the path waypoints and start zombie walking
	waypoints = path:GetWaypoints()
	-- Move to first waypoint
	currentWaypointIndex = 1
	humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
	-- Error (path not found); stop humanoid
	humanoid:MoveTo(zombie.HumanoidRootPart.Position)
end

end

local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex = currentWaypointIndex + 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
end

local function onPathBlocked(blockedWaypointIndex)
– Check if the obstacle is further down the path
if blockedWaypointIndex > currentWaypointIndex then
– Call function to re-compute the path
followPath(destination)
end
end

– Connect ‘Blocked’ event to the ‘onPathBlocked’ function
path.Blocked:Connect(onPathBlocked)

– Connect ‘MoveToFinished’ event to the ‘onWaypointReached’ function
humanoid.MoveToFinished:Connect(onWaypointReached)

followPath(destination)`

4 Likes

i think the error its you trying to store the waypoints

1 Like

It’s not, it works, but it is very chunky and the NPC can be pushed!

1 Like

bodyforce to solve? i think the problem can be this

1 Like

Where is bodyforce? I would like to know!

Bodyforce will not prevent players from pushing your NPC. And BodyForce as the name suggests, takes in a force and uses it on the object. A body force is basically a player pushing the NPC around but you’re scripting it.

If you want a real solution, use tweening. The Roblox TweenService will ensure smooth movement to your desired point. You can check every time the tween is finished I believe and run one on the next point. An anchored object will not be pushed around by a player if you use the TweenService. However will using the tween service I’m not sure about any animations running unless you forced them on since it’s not using the roblox movement.

1 Like

And How would I do checkpoints simply and easily? I don’t understand the other methods.

How are you storing your locations for the character to move?

i would like to learn about tweening service because i only use basic and semi-advanced

You could turn can collide off for all the parts in your npc

It’d just fall through the ground at that point.

I just give them a model, with a primary part to move to.

but, can use player collisions?

Also, do we tween through C-Frame?

You need to have all your points for the NPC to move to in order for it to walk around in any area. Once you have those points you can use the TweenService to move the model to that point then to the next and next until you reach your target. Do a loop over and when the Tween is done, move on to the next. Make sure to specify the order.

The TweenService is completely different. TweenService you tell it to run once and it does it smoothly for you. CFrame you have to do a loop and update the position.

Okay, how would I store the nodes, can you make an example script of how I would path-find this? Me and my friends are new to pathfinding, and storing nodes.

but it can use Player’s collision to an specific collision :slight_smile:

Thats not using tweening… I don’t believe thats correct!

Also, I would like to place checkpoints for it to follow!