Trying to use Pathfinding for a game

Recently, I’ve been attempting to script a game with Pathfinding, but have been having some issues. I can’t seem to find any good tutorials for parts of it. My friend has helped me do what I have written so far, but I still have other questions.

Here is the script:

Local PathfindingService = game:GetService(“PathfindingService”)

local human = script.Parent:WaitForChild(“Humanoid”)
local Torso = script.Parent:WaitForChild(“Torso”)

local path = PathfindingService:CreatePath()
path:ComputeAsync(Torso.Position, game.Workspace.EndingPart.Position)
local waypoints = path:GetWaypoints()
for i, waypoints in pairs(waypoints) do
human:MoveTo(waypoints.Position)
human.MoveToFinished:Wait(2)

end

human:MoveTo(game.Workspace.EndingPart.Position)

How can I do this with an R15 character, and how would I go about adding animations to it?

1 Like

First, are there any errors in the output? I can already see the possible cause of one.

Is your game R15? Because, if it is, the “Torso” isn’t a part of the R15 Rig, it’s now HumanoidRootPart.
Another error, is that in the for i,waypoints loop, you put the Value’s name to waypoints, but that’s also the name of the Waypoints table, so change your loop to something like this.

for i, waypoint do(waypoints) do
human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait()
end

And for adding animations, first setup an animation with

local animation = Instance.new(“Animation”)
animation.AnimationId = “put your Animation ID here”
local animationTrack = human:LoadAnimation(animation)

Once you’ve done that, you would Play it right before the

human:MoveTo(waypoint.Position)

by doing

animationTrack:Play()

That should solve your problem :+1:

1 Like

Thank you so much for the help! Everything works, but the walking animation seems to have a small issue.The starting and stopping of the animation isn’t smooth, and I’m not exactly sure how to explain the error.

Edit: With a little bit of troubleshooting, I’ve noticed it only happens when he reaches a new point of the Pathfollowing trail (or whatever it’s called).

1 Like

Problem solved thanks to DaErrorz. Just had to move the “animationTrack:Play()” above the loop as I think he intended for me to do in the first place.

1 Like

Yeah, I meant that :+1: . If you need anything, I’m always free :grinning_face_with_smiling_eyes:

1 Like