Advertisement
DringDringDring

Untitled

Sep 21st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. --[[
  2. A.I./I.A System/Système
  3. internetExplorerchad
  4. --]]
  5. local alien = {}
  6. local GameLoop = require(game.ServerScriptService["Game Loop"])
  7.  
  8. function alien.new(health,walkSpeed)
  9.     local obj = setmetatable({
  10.         currentTarget = nil,
  11.         model = script.Alien:Clone(),
  12.         targetDistance = 1e10,
  13.         deathConnection = nil
  14.     },{__index=alien})
  15.     obj.model.Humanoid.MaxHealth = health or 100
  16.     obj.model.Humanoid.Health = health or 100
  17.     obj.model.Humanoid.WalkSpeed = walkSpeed or 16
  18.     return obj
  19. end
  20.  
  21. local pathfindingService = game:GetService("PathfindingService")
  22. function alien:walkTo()
  23.     if self.currentTarget and self.currentTarget.Parent == workspace then
  24.         local path = pathfindingService:CreatePath({AgentRadius = 2, AgentHeight = 5, AgentCanJump = true})
  25.         path:ComputeAsync(self.model.PrimaryPart.Position,self.currentTarget.PrimaryPart.Position)
  26.         local waypoints = path:GetWaypoints()
  27.         for x = 1,#waypoints-1,1 do
  28.             local ray1 = Ray.new(self.model.PrimaryPart.Position,waypoints[x].Position)
  29.             local ray2 = Ray.new(self.currentTarget.PrimaryPart.Position,waypoints[x].Position)
  30.             local hit1 = workspace:FindPartOnRay(ray1,self.model.PrimaryPart)
  31.             local hit2 = workspace:FindPartOnRay(ray2,self.currentTarget.PrimaryPart)
  32.             if not hit1 and not hit2 then
  33.                 print("...")
  34.                 self.model.Humanoid:MoveTo(waypoints[x].Position)
  35.                
  36.                 local t = (waypoints[x].Position-self.model.PrimaryPart.Position).magnitude/self.model.Humanoid.WalkSpeed
  37.                
  38.                 local bind = Instance.new("BindableEvent")
  39.                 local event = bind.Event
  40.                 local hasFired = false
  41.                
  42.                 do
  43.                     spawn(function()
  44.                         local hasReached = self.model.Humanoid.MoveToFinished:Wait()
  45.                         if not hasFired then
  46.                             hasFired = true
  47.                             bind:Fire("Success")
  48.                         end
  49.                     end)
  50.                     delay(t,function()
  51.                         if not hasFired then
  52.                             hasFired = true
  53.                             bind:Fire("Error : has not been reached within time")
  54.                         end
  55.                     end)
  56.                 end
  57.                
  58.                 local result = event:Wait()
  59.                 print(result)
  60.                 if result == "Error : has not been reached within time" then
  61.                     break
  62.                 end
  63.             else
  64.                 print(",,,")
  65.                 local t = (self.currentTarget.PrimaryPart.Position-self.model.PrimaryPart.Position).magnitude/self.model.Humanoid.WalkSpeed
  66.                
  67.                 self.model.Humanoid:MoveTo(self.currentTarget.PrimaryPart.Position,self.currentTarget.PrimaryPart)
  68.                
  69.                 local bind = Instance.new("BindableEvent")
  70.                 local event = bind.Event
  71.                 local hasFired = false
  72.                
  73.                 do
  74.                     spawn(function()
  75.                         local hasReached = self.model.Humanoid.MoveToFinished:Wait()
  76.                         if not hasFired then
  77.                             hasFired = true
  78.                             bind:Fire("Success")
  79.                         end
  80.                     end)
  81.                     delay(t,function()
  82.                         if not hasFired then
  83.                             hasFired = true
  84.                             bind:Fire("Error : has not been reached within time")
  85.                         end
  86.                     end)
  87.                 end
  88.                
  89.                 local result = event:Wait()
  90.                 print(result)
  91.                 if result == "Error : has not been reached within time" then
  92.                     break
  93.                 end
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. function alien:chase()
  100.     self.model.Humanoid.Died:Connect(function()
  101.         local modelSave = self.model
  102.         self = nil
  103.         modelSave:Destroy()
  104.         return
  105.     end)
  106.    
  107.     local waterJumpDebounce = false
  108.     self.model.Humanoid.Swimming:Connect(function()
  109.         if waterJumpDebounce == false then
  110.             waterJumpDebounce = true
  111.             self.model.PrimaryPart.Velocity = self.model.PrimaryPart.Velocity + Vector3.new(0,100,0)
  112.             delay(wait(),function() waterJumpDebounce = false end)
  113.         end
  114.     end)
  115.    
  116.     repeat
  117.         for _,v in pairs(game.Players:GetPlayers()) do
  118.             if GameLoop.activePlayers[v.Name] and v.Character and self.model.Humanoid.Health > 0 and v.Character.Humanoid.Health > 0 then
  119.                 local mag = (v.Character.PrimaryPart.Position-self.model.PrimaryPart.Position).magnitude
  120.                 if mag < self.targetDistance then
  121.                     self.targetDistance = mag
  122.                     self.currentTarget = v.Character
  123.                     self.deathConnection = v.Character.Humanoid.Died:Connect(function()
  124.                         self.targetDistance = 1e10
  125.                         self.currentTarget  = nil
  126.                         self.deathConnection = self.deathConnection and self.deathConnection:Disconnect()
  127.                         self.deathConnection = nil
  128.                         self:chase()
  129.                         return
  130.                     end)
  131.                 end
  132.             end
  133.         end
  134.         --print("Alien is currently chasing ", self.currentTarget)
  135.         self:walkTo()
  136.         wait(1)
  137.     until 5 == 6
  138. end
  139.  
  140. return alien
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement