local Player = game.Players.LocalPlayer
local PFS = game:GetService("PathfindingService")
local Start = Vector3.new(0,0,0)
local End = Vector3.new(10,0,0)
local = Player.Character
Repeat task.wait() until Char:FindFirstChild("Humanoid")
Char:MoveTo(Start)
local Hum = Char.Humanoid
PFS:ComputeAsync(Start, End)
Hum:MoveTo(End)
Hum:MoveToFinished:Wait()
Print("Moved to ".. tostring(End))
Hello, this is my first time using pathfinding.
I made this code to test out pathfinding.
why isn’t it working? I’m trying to make the local player walk to end from start
Well no it’s not, your script and roblox’s script are very different.
For starters, you never create a path object with PathFindingService:CreatePath()
You aren’t using the waypoints
Humanoid move to finished is an event so you use . not :
I’d recommend reading it as it’s designed to be more user friendly than the api
Hey, the article doesnt help too much when it comes to writing the actual code. It helps me visually understand it and know how it works but I don’t really know how to do it in terms of writing the code. Showing me how to actually write it would help.
Sure, I made this simple script:
How it works is you create the path initially with the agent parameters (agent is the rig you are moving)
The parameters are pretty self explanatory apart from costs, which makes the agent prefer to walk on certain materials more than others. So you could make it prefer walking on grass than lava for example.
Then compute the path with the start and end position, which will either be successful or failed
You can then loop through the waypoints to move the agent
--// Services
local PathfindingService = game:GetService('PathfindingService')
--// Variables
local path : Path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Water = 20
}
})
local rig = workspace.Dummy
local humanoid = rig.Humanoid
local destination = Vector3.new(0, 0, 20)
--// Main
local success, result = pcall(function()
path:ComputeAsync(rig.PrimaryPart.Position, destination)
end)
if (success) and (path.Status == Enum.PathStatus.Success) then
for _, waypoint : PathWaypoint in pairs(path:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
print('Finished path!')
else
warn('Failed to compute the path.')
end