Help with Pathfinding

Helloo!

I’m just starting out with scripting and I was practicing some pathfinding. I had no errors in the script itself, I anchored all parts except for the NPC and somehow this happened:

https://streamable.com/o17smn

How can I fix this?

Script:

-- get pathfinding service
local pathfindingService = game:GetService("PathfindingService")
 
-- Variables for NPC humanoid, torso, and destination
local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")
local destination = game.Workspace.Destination.Position
 
-- create path object
local path = pathfindingService:CreatePath()
 
-- compute a path
path:ComputeAsync(body.Position, destination)
 
-- get the waypoints table
local waypoints = path:GetWaypoints()
 
-- iterate through all waypoints, and jump when necessary
for k, waypoint in pairs(waypoints) do
    humanoid:MoveTo(waypoint.Position)
   
    -- change humanoid state to jump if necessary
    if waypoint.Action == Enum.PathWaypointAction.Jump then
        humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    end
   
    humanoid.MoveToFinished:Wait()
end

It might help if you showed us the script.

Wow thats crazy, looks like the player has the ability to activate a jump before they’re back on the ground.
In any result u can use this method to limit that behavior for brief periods off the ground. But yeah, it’d be helpful to show the code u have, especially the moving logic between waypoints.

Here’s your issue:

When a humanoid jumps, the humanoid state is set to Jumping, and a force is applied opposite to gravity for a short amount of time. Then, the humanoid state is set to Freefall, and the force is removed. By forcibly changing the state to Jumping in this part of your code, if the humanoid was in the Freefall state following a jump, it would be overwritten, and the humanoid would again jump, leaving you with the Flappy Bird pathfinding you have there.

To fix it, simply replace that line of code with this, and let Roblox’s internals handle the state changes involved with jumping.

humanoid.Jump = true
2 Likes