RobloxDel

Npc Wandering Script (Fixed)

Sep 28th, 2019 (edited)
8,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | Gaming | 0 0
  1. local humanoid = script.Parent.Humanoid
  2. local torso = script.Parent.HumanoidRootPart
  3. local ps = game:GetService("PathfindingService")
  4.  
  5. math.randomseed(tick())--create seed
  6.  
  7. while wait(1) do
  8.     local pos = torso.CFrame.Position + Vector3.new(math.random(-50,50),0,math.random(-50,50))--100*100 block radius
  9.     local path = ps:CreatePath({AgentHeight = 1, AgentRadius = 1})
  10.     local comp = path:ComputeAsync(torso.CFrame.Position, pos)
  11.  
  12.     if path.Status == Enum.PathStatus.Success then
  13.         local waypoints = path:GetWaypoints()
  14.  
  15.         humanoid.Seated:Connect(function(seated)
  16.             if seated == true then
  17.                 print("seated")
  18.                 humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  19.             end
  20.         end)
  21.  
  22.         path.Blocked:Connect(function()
  23.             print("blocked!")
  24.             humanoid:MoveTo(torso.CFrame.Position)
  25.         end)
  26.  
  27.         for _,point in pairs(waypoints) do
  28.             humanoid:MoveTo(point.Position)
  29.  
  30.             --This is just a demonstration of where the point is, you can remove it
  31.             local part = Instance.new("Part")
  32.  
  33.             part.CanCollide = false
  34.             part.Locked = true
  35.             part.Anchored = true
  36.             part.Material = Enum.Material.Neon
  37.             part.Size = Vector3.new(1,0.5,1)
  38.             part.CFrame = CFrame.new(point.Position)
  39.             part.Transparency = 0.25
  40.             part.Parent = workspace
  41.            
  42.             if point.Action == Enum.PathWaypointAction.Jump then
  43.                 print("Jumping")
  44.                 humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  45.             end
  46.            
  47.             humanoid.MoveToFinished:Wait()
  48.            
  49.             part:Destroy()
  50.         end
  51.     end
  52.    
  53.     wait(math.random(1, 4))
  54. end
Advertisement
Add Comment
Please, Sign In to add comment