Sir_Spaceboi

w/ raycast need help

Nov 8th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. wait(2)
  2.  
  3. local PathfindingService = game:GetService("PathfindingService")
  4. local CustomerFolder = game.ReplicatedStorage.NPCs
  5. local SpawnsFolder = workspace.NPC.Spawns
  6. local locationsFolder = workspace.NPC.Locations
  7.  
  8. while true do
  9. -- Get the random NPC
  10. local children = CustomerFolder:GetChildren()
  11. local random_customer = children[math.random(1, #children)]
  12.  
  13. -- Find a spawn for it
  14. local spawns = SpawnsFolder:GetChildren()
  15. local random_spawn = spawns[math.random(#spawns)]
  16.  
  17. -- Calculate a random destination
  18. local locations = locationsFolder:GetChildren()
  19. local random_location = locations[math.random(#locations)]
  20.  
  21. -- Variables
  22. local npc = CustomerFolder:FindFirstChild(random_customer.Name)
  23.  
  24. local npcClone = npc:Clone()
  25. warn("NPC Inserted | Name: " .. npcClone.Name)
  26.  
  27. npcClone.Parent = workspace.NPC.NPCs
  28. npcClone:SetPrimaryPartCFrame(random_spawn.CFrame)
  29.  
  30. local humanoid = npcClone.Humanoid
  31. local root = npcClone.HumanoidRootPart
  32.  
  33. local destination = random_location
  34.  
  35. for _, NPC in pairs(workspace.NPC.NPCs:GetChildren()) do
  36. warn("NPCS | NPCs currently in game: ")
  37. for i,v in pairs(workspace.NPC.NPCs:GetChildren()) do
  38. print(v)
  39. end
  40. if NPC:FindFirstChild("Humanoid") then
  41. -- Optimize the NPC movement
  42. for index, item in pairs(NPC:GetChildren()) do
  43. if item:IsA("BasePart") then
  44. item:SetNetworkOwner(nil)
  45. end
  46. end
  47.  
  48. local randomNum = math.random(0, 10)
  49.  
  50. NPC.Patience.Value = NPC.Patience.Value + randomNum;
  51. if NPC.Patience.Value >= 10 then
  52. local leavePath = PathfindingService:CreatePath()
  53. leavePath:ComputeAsync(NPC.HumanoidRootPart.Position, workspace.NPC.LeaveDestination.Position);
  54.  
  55. local waypoints = leavePath:GetWaypoints()
  56.  
  57. if leavePath.Status == Enum.PathStatus.Success then
  58. for _, waypoint in pairs(waypoints) do
  59. if waypoint.Action == Enum.PathWaypointAction.Jump then
  60. humanoid.Jump = true
  61. end
  62. humanoid:MoveTo(waypoint.Position)
  63. delay(0.5, function()
  64. if humanoid.WalkToPoint.Y > NPC.HumanoidRootPart.Position.Y then
  65. humanoid.Jump = true
  66. end
  67. end)
  68. humanoid.MoveToFinished:Wait()
  69. end
  70. else
  71. print("Path didn't come out right!")
  72. end
  73. wait(5)
  74. NPC:Destroy()
  75. wait("NPC Removed | " .. NPC.Name)
  76. end
  77. local humanoid = NPC:WaitForChild("Humanoid")
  78. local randomLocation = locations[math.random(#locations)]
  79. local newPath = PathfindingService:CreatePath()
  80. newPath:ComputeAsync(NPC.HumanoidRootPart.Position, randomLocation.Position)
  81.  
  82. local waypoints = newPath:GetWaypoints()
  83.  
  84. if newPath.Status == Enum.PathStatus.Success then
  85. for _, waypoint in pairs(waypoints) do
  86. if waypoint.Action == Enum.PathWaypointAction.Jump then
  87. humanoid.Jump = true
  88. end
  89. humanoid:MoveTo(waypoint.Position)
  90.  
  91. local raycastParams = RaycastParams.new()
  92. raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
  93. raycastParams.FilterDescendantsInstances = {workspace.Model}
  94. raycastParams.IgnoreWater = true
  95.  
  96. local part = NPC:WaitForChild("Head")
  97. local raycastResult = workspace:Raycast(part.Position, part.CFrame.LookVector*50, raycastParams)
  98.  
  99. if raycastResult then
  100. local raycastResultPosition = raycastResult.Position
  101. if raycastResultPosition.Magnitude >= 10 then
  102. -- i need to re-direct the NPC but idk how
  103. end
  104. end
  105.  
  106. delay(0.5, function()
  107. if humanoid.WalkToPoint.Y > NPC.HumanoidRootPart.Position.Y then
  108. humanoid.Jump = true
  109. end
  110. end)
  111. humanoid.MoveToFinished:Wait()
  112. end
  113. else
  114. print("Path didn't come out right!")
  115. end
  116. end
  117. end
  118.  
  119. -- Create the path object
  120. local path = PathfindingService:CreatePath()
  121. local path2 = PathfindingService:CreatePath()
  122.  
  123. -- Table of Orders
  124. local PossibleOrders = {
  125. "Watermelon Smoothie with a Vanilla Ice cream",
  126. "Strawberry Ice cream with Sprinkles",
  127. "Large Latte and a Cookie Dough Ice cream"
  128. }
  129.  
  130. -- Compute the path
  131. path:ComputeAsync(npcClone.HumanoidRootPart.Position, destination.Position)
  132.  
  133. -- Get the path waypoints
  134. local waypoints = path:GetWaypoints()
  135. local waypoints2 = path2:GetWaypoints()
  136.  
  137. -- Optimize the NPC movement
  138. for index, item in pairs(npcClone:GetChildren()) do
  139. if item:IsA("BasePart") then
  140. item:SetNetworkOwner(nil)
  141. end
  142. end
  143.  
  144. --
  145.  
  146. if path.Status == Enum.PathStatus.Success then
  147. for _, waypoint in pairs(waypoints) do
  148. if waypoint.Action == Enum.PathWaypointAction.Jump then
  149. humanoid.Jump = true
  150. end
  151. humanoid:MoveTo(waypoint.Position)
  152. delay(0.5, function()
  153. if humanoid.WalkToPoint.Y > root.Position.Y then
  154. humanoid.Jump = true
  155. end
  156. end)
  157. humanoid.MoveToFinished:Wait()
  158. end
  159. else
  160. print("Path didn't come out right!")
  161. end
  162. wait(35)
  163. end
Advertisement
Add Comment
Please, Sign In to add comment