Advertisement
Xmaybeu

AI Script

Apr 15th, 2025
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | Source Code | 0 0
  1. local PathfindingService = game:GetService("PathfindingService")
  2.  
  3. local Waypoints = workspace.Waypoints:GetChildren()
  4.  
  5. local AI = script.Parent
  6. local Humanoid = AI.Humanoid
  7. local HumanoidRootPart = AI.HumanoidRootPart
  8. local State = AI.Status
  9. local MovementMode = AI.MovementMode
  10.  
  11. local currentlyMoving = false
  12. local breakCurrentWaypoint = false
  13.  
  14. local movingFinished = script.MovingFinished
  15. local breakMoving = script.BreakMoving
  16.  
  17. local raycastParams = RaycastParams.new()
  18. raycastParams.FilterType = Enum.RaycastFilterType.Exclude
  19. raycastParams.FilterDescendantsInstances = {AI}
  20.  
  21. local lastwaypoint : BasePart = workspace.Baseplate
  22.  
  23. function getRandomWaypoint() : BasePart & boolean
  24.     local randomWaypoint : BasePart = Waypoints[math.random(1, #Waypoints)]
  25.    
  26.     if randomWaypoint.Name == lastwaypoint.Name then
  27.         return getRandomWaypoint()
  28.     end
  29.    
  30.     local inSight = waypointInSight(randomWaypoint)
  31.     lastwaypoint = randomWaypoint
  32.    
  33.     return randomWaypoint, inSight
  34. end
  35.  
  36. function waypointInSight(waypoint : BasePart)
  37.     local direction = (waypoint.Position - HumanoidRootPart.Position).Unit * 1000
  38.  
  39.     local raycast = workspace:Raycast(HumanoidRootPart.Position, direction, raycastParams)
  40.        
  41.     if raycast then
  42.         if string.find(raycast.Instance.Name, "Waypoint") then
  43.             return true
  44.         else
  45.             return false
  46.         end
  47.     else
  48.         return false
  49.     end
  50. end
  51.  
  52. --[[
  53. function waypointIsBlocked(currentWaypoint: BasePart) : boolean
  54.     local direction = (currentWaypoint.Position - HumanoidRootPart.Position).Unit * 1000
  55.    
  56.     local raycast = workspace:Raycast(HumanoidRootPart.Position, direction, raycastParams)
  57.     if raycast then
  58.         if raycast.Instance.Name == "WaypointPosition" then
  59.             return false
  60.         else
  61.             return true
  62.         end
  63.     else
  64.         return false
  65.     end
  66. end]]
  67.  
  68. function makePartOutOfWaypoint(waypointPosition: Vector3) : BasePart
  69.     local part = Instance.new("Part", AI)
  70.     part.Transparency = 1
  71.     part.CanCollide = false
  72.     part.Anchored = true
  73.     part.Position = waypointPosition
  74.     part.Name = "WaypointPosition"
  75.     part.Size = Vector3.one
  76.    
  77.     return part
  78. end
  79.  
  80. function changeStatsByState()
  81.     if State.Value == "Walking" then
  82.         Humanoid.WalkSpeed = 14
  83.     elseif State.Value == "Running" then
  84.         Humanoid.WalkSpeed = 20
  85.     end
  86. end
  87.  
  88. function chooseBehaviorAfterAction()
  89.     local actions = {"BeIdle", "Continue"}
  90.    
  91.     local randomAction = actions[math.random(1, #actions)]
  92.    
  93.     return randomAction
  94. end
  95.  
  96. function moveToWaypoint(waypoint: BasePart?, regeneratePath: boolean?)
  97.     if not currentlyMoving then
  98.        
  99.         local waypoint, inSight = getRandomWaypoint()
  100.  
  101.         if waypoint then
  102.             if inSight then
  103.                 currentlyMoving = true
  104.                 Humanoid:MoveTo(waypoint.Position, waypoint)
  105.                 Humanoid.MoveToFinished:Wait()
  106.                 currentlyMoving = false
  107.             else
  108.                 local path = PathfindingService:CreatePath({
  109.                     AgentRadius = 2,
  110.                     AgentHeight = 5,
  111.                     AgentCanJump = false,
  112.                     AgentCanClimb = false
  113.                 })
  114.  
  115.                 local success, errorMessage = pcall(function()
  116.                     path:ComputeAsync(HumanoidRootPart.Position, waypoint.Position)
  117.                 end)
  118.  
  119.                 if success and path.Status == Enum.PathStatus.Success then
  120.                     currentlyMoving = true
  121.                    
  122.                     State.Value = "Walking"
  123.  
  124.                     for _, pathWaypoint in ipairs(path:GetWaypoints()) do
  125.                         local pW_part = makePartOutOfWaypoint(pathWaypoint.Position)
  126.                        
  127.                         if breakCurrentWaypoint then
  128.                             break
  129.                         end
  130.                        
  131.                         if waypointInSight(waypoint) then
  132.                             MovementMode.Value = "Direct"
  133.                            
  134.                             Humanoid:MoveTo(pW_part.Position)
  135.                             Humanoid.MoveToFinished:Wait()
  136.                            
  137.                             break
  138.                         else
  139.                             if MovementMode.Value ~= "PathDependant" then
  140.                                 MovementMode.Value = "PathDependant"
  141.                             end
  142.                            
  143.                             Humanoid:MoveTo(pW_part.Position)
  144.                            
  145.                             breakMoving.Event:Connect(function()
  146.                                 breakCurrentWaypoint = true
  147.                             end)
  148.                            
  149.                             if breakCurrentWaypoint then
  150.                                 break
  151.                             end
  152.                            
  153.                             Humanoid.MoveToFinished:Wait()
  154.                         end
  155.                         pW_part:Destroy()
  156.                     end
  157.                    
  158.                     movingFinished:Fire()
  159.                     currentlyMoving = false
  160.                     State.Value = "Idle"
  161.                 else
  162.                     State.Value = "Idle"
  163.                 end
  164.             end
  165.         end
  166.     end
  167. end
  168.  
  169. function update()
  170.     moveToWaypoint()
  171.    
  172.     local action = chooseBehaviorAfterAction()
  173.     if action then
  174.         if action == "BeIdle" then
  175.             State.Value = "Idle"
  176.             task.wait(5)
  177.         elseif action == "Continue" then
  178.             State.Value = "Walking"
  179.         end
  180.     end
  181.    
  182.     update()
  183. end
  184.  
  185. update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement