🗺️

Roblox Path Finding Service Tutorial

Aug 8, 2024

Roblox Scripting Tutorial: Path Finding Service

Introduction

  • Presenter: Defconn Gear
  • Topic: Path Finding Service in Roblox

What is Path Finding Service?

  • Allows a character to navigate a calculated path.
  • Example demonstrated: Character moves along a path to a specified point.

Setting Up

  1. Create a Baseplate
    • Insert a character to move around.
  2. Inserting a Character
    • Use Roblox Toolbox to find a free NPC or use the "Load Character" plugin.
    • Ensure the character has a humanoid to utilize movement functions.

Scripting the Path Finding

Steps to Script

  1. Insert a Script
    • Name it "Pathfinding Script".
  2. Get Path Finding Service
    local pathFindingService = game:GetService("PathfindingService")
    
  3. Define Humanoid and Torso
    local human = script.Parent:WaitForChild("Humanoid")
    local torso = script.Parent:WaitForChild("Torso")
    
  4. Creating the Path
    local path = pathFindingService:CreatePath()
    

Computing the Path

  • Use ComputeAsync to define start and end points.
    path:ComputeAsync(torso.Position, game.Workspace.EndingPart.Position)
    

Moving the Character Along the Path

  1. Get Waypoints
    local waypoints = path:GetWaypoints()
    
  2. Loop Through Each Waypoint
    for i, waypoint in ipairs(waypoints) do
        human:MoveTo(waypoint.Position)
        human.MoveToFinished:Wait()
    end
    

Handling Obstacles

  • If the path encounters a wall, it will get stuck unless it calculates the new path.
  • Handle blocked paths using path.Status and PathBlocked.

Testing and Improvements

  • Ensure the character can jump over obstacles by checking the waypoint action.
    if waypoint.Action == Enum.PathWaypointAction.Jump then
        human:ChangeState(Enum.HumanoidStateType.Jumping)
    end
    

Visualizing the Path

  • Create visual indicators for waypoints as spheres or blocks using loops:
    local part = Instance.new("Part")
    part.Size = Vector3.new(1, 1, 1)
    part.Material = Enum.Material.Neon
    part.Position = waypoint.Position + Vector3.new(0, 2, 0)
    part.Parent = game.Workspace
    

Conclusion

  • Recap of pathfinding functionality and how to implement it in Roblox.
  • Encourage viewers to explore further tutorials and join the scripting community.
  • Reminder to like, subscribe, and check the description for resources.