Advertisement
Sir_Spaceboi

npc script

Nov 8th, 2021
4,738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.24 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.         if NPC:FindFirstChild("Humanoid") then
  37.             -- Optimize the NPC movement
  38.             for index, item in pairs(NPC:GetChildren()) do
  39.                 if item:IsA("BasePart") then
  40.                     item:SetNetworkOwner(nil)
  41.                 end
  42.             end
  43.            
  44.             local randomNum = math.random(0, 10)
  45.            
  46.             NPC.Patience.Value = NPC.Patience.Value + randomNum;
  47.             if NPC.Patience.Value >= 10 then
  48.                 local leavePath = PathfindingService:CreatePath()
  49.                 leavePath:ComputeAsync(NPC.HumanoidRootPart.Position, workspace.NPC.LeaveDestination.Position);
  50.                
  51.                 local waypoints = leavePath:GetWaypoints()
  52.                
  53.                 if leavePath.Status == Enum.PathStatus.Success then
  54.                     for _, waypoint in pairs(waypoints) do 
  55.                         if waypoint.Action == Enum.PathWaypointAction.Jump then
  56.                             humanoid.Jump = true
  57.                         end
  58.                         humanoid:MoveTo(waypoint.Position)
  59.                         delay(0.5, function()
  60.                             if humanoid.WalkToPoint.Y > NPC.HumanoidRootPart.Position.Y then
  61.                                 humanoid.Jump = true
  62.                             end
  63.                         end)
  64.                         humanoid.MoveToFinished:Wait()
  65.                     end
  66.                 else
  67.                     print("Path didn't come out right!")
  68.                 end
  69.                 wait(5)
  70.                 NPC:Destroy()
  71.                 wait("NPC Removed | " .. NPC.Name)
  72.             end
  73.             local humanoid = NPC:WaitForChild("Humanoid")
  74.             local randomLocation = locations[math.random(#locations)]
  75.             local newPath = PathfindingService:CreatePath()
  76.             newPath:ComputeAsync(NPC.HumanoidRootPart.Position, randomLocation.Position)
  77.  
  78.             local waypoints = newPath:GetWaypoints()
  79.  
  80.             if newPath.Status == Enum.PathStatus.Success then
  81.                 for _, waypoint in pairs(waypoints) do 
  82.                     if waypoint.Action == Enum.PathWaypointAction.Jump then
  83.                         humanoid.Jump = true
  84.                     end
  85.                     humanoid:MoveTo(waypoint.Position)
  86.                     delay(0.5, function()
  87.                         if humanoid.WalkToPoint.Y > NPC.HumanoidRootPart.Position.Y then
  88.                             humanoid.Jump = true
  89.                         end
  90.                     end)
  91.                     humanoid.MoveToFinished:Wait()
  92.                 end
  93.             else
  94.                 print("Path didn't come out right!")
  95.             end
  96.         end
  97.     end
  98.  
  99.     -- Create the path object
  100.     local path = PathfindingService:CreatePath()
  101.     local path2 = PathfindingService:CreatePath()
  102.  
  103.     -- Table of Orders
  104.     local PossibleOrders = {
  105.         "Watermelon Smoothie with a Vanilla Ice cream",
  106.         "Strawberry Ice cream with Sprinkles",
  107.         "Large Latte and a Cookie Dough Ice cream"
  108.     }
  109.  
  110.     -- Compute the path
  111.     path:ComputeAsync(npcClone.HumanoidRootPart.Position, destination.Position)
  112.  
  113.     -- Get the path waypoints
  114.     local waypoints = path:GetWaypoints()
  115.     local waypoints2 = path2:GetWaypoints()
  116.  
  117.     -- Optimize the NPC movement
  118.     for index, item in pairs(npcClone:GetChildren()) do
  119.         if item:IsA("BasePart") then
  120.             item:SetNetworkOwner(nil)
  121.         end
  122.     end
  123.  
  124.     --
  125.  
  126.     if path.Status == Enum.PathStatus.Success then
  127.         for _, waypoint in pairs(waypoints) do 
  128.             if waypoint.Action == Enum.PathWaypointAction.Jump then
  129.                 humanoid.Jump = true
  130.             end
  131.             humanoid:MoveTo(waypoint.Position)
  132.             delay(0.5, function()
  133.                 if humanoid.WalkToPoint.Y > root.Position.Y then
  134.                     humanoid.Jump = true
  135.                 end
  136.             end)
  137.             humanoid.MoveToFinished:Wait()
  138.         end
  139.     else
  140.         print("Path didn't come out right!")
  141.     end
  142.    
  143.     warn("NPCS | NPCs currently in game: ")
  144.     for i,v in pairs(workspace.NPC.NPCs:GetChildren()) do
  145.         print(v)
  146.     end
  147.     wait(35)
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement