Advertisement
oopsrainbow4

PathFinding

Feb 28th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. -- Dummy need to be unanchor all parts.
  2.  
  3. local humanoid = script.Parent.Humanoid
  4. local hrp = script.Parent.HumanoidRootPart
  5.  
  6. local pathfindingService = game:GetService("PathfindingService")
  7.  
  8. wait(20)
  9.  
  10. local function showPath(path)
  11.     local waypoints = path:GetWaypoints()
  12.     for _, waypoint in ipairs(waypoints) do
  13.         local part = Instance.new("Part", game.Workspace)
  14.         part.Shape = Enum.PartType.Ball
  15.         part.Material = Enum.Material.Neon
  16.         part.Anchored = true
  17.         part.CanCollide = false
  18.         part.Size = Vector3.new(0.6, 0.6, 0.6)
  19.         part.Position = waypoint.Position
  20.         game:GetService("Debris"):AddItem(part, 5) -- Deleted Parts in 5 seconds
  21.     end
  22. end
  23.  
  24. local function createPath(target)
  25.     local agentParams = {
  26.         AgentHeight = 2.5,
  27.         AgentRadius = 2,
  28.         AgentCanJump = true
  29.     }
  30.     local path = pathfindingService:CreatePath(agentParams)
  31.     path:ComputeAsync(hrp.Position, target.Position)
  32.     if path.Status == Enum.PathStatus.Success then
  33.         local waypoints = path:GetWaypoints()
  34.         return path, waypoints
  35.     end
  36. end
  37.  
  38. local function main()
  39.     local target = game.Workspace.EndPart
  40.     if target then
  41.         local path, waypoints = createPath(target)
  42.         if path and waypoints then
  43.             local showThePath = showPath(path)
  44.             for _, waypoint in ipairs(waypoints) do
  45.                 if waypoint.Action == Enum.PathWaypointAction.Jump then
  46.                     humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  47.                 end
  48.                 humanoid:MoveTo(waypoint.Position)
  49.                 humanoid.MoveToFinished:Wait(2)
  50.             end
  51.         end
  52.     end
  53. end
  54.  
  55. while wait() do main() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement