Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. local turret = {}
  2.  
  3. local debris = game:GetService("Debris")
  4.  
  5. function turret.startTurret(part)
  6.     local lastShot = tick()
  7.     while true do
  8.         local lastMag, object = 200, nil
  9.         for i,v in pairs(workspace.Zombies:GetChildren()) do
  10.             local magnitude = (v.HumanoidRootPart.Position - part.Position).magnitude
  11.             if magnitude < lastMag then
  12.                 lastMag = magnitude
  13.                 object = v
  14.             end
  15.         end
  16.         if lastMag < 50 then
  17.             if object then
  18.                 part.Parent:SetPrimaryPartCFrame(CFrame.new(part.Position, Vector3.new(object.UpperTorso.Position.X, part.Position.Y, object.UpperTorso.Position.Z))); wait()
  19.             end
  20.         end
  21.         if tick() - lastShot >= 1 then
  22.             if object and object:FindFirstChild("Humanoid") then
  23.                 if object.Humanoid.Health > 0 then
  24.                     if lastMag < 50 then -- LAST MAG CAUSES ISSUES AND DELAYS SHOOTING. FIND OUT WHY
  25.                         local muzzle = part.Parent.Muzzle
  26.                         local ray = Ray.new(muzzle.CFrame.p, (Vector3.new(object.HumanoidRootPart.Position.X, muzzle.Position.Y, object.HumanoidRootPart.Position.Z) - muzzle.CFrame.p).unit * 300)
  27.                         local part, position = workspace:FindPartOnRayWithIgnoreList(ray, {game.Players:GetPlayers()[1].Character, workspace.LevelGeometry}, false, true)
  28.                        
  29.                         if part:IsDescendantOf(workspace.Zombies) then
  30.                             part.Parent.Humanoid:TakeDamage(8)
  31.                            
  32.                             local beam = Instance.new("Part", workspace)
  33.                             beam.BrickColor = BrickColor.new("Deep orange")
  34.                             beam.FormFactor = "Custom"
  35.                             beam.Material = "Neon"
  36.                             beam.Transparency = 0.2
  37.                             beam.Anchored = true
  38.                             beam.Locked = true
  39.                             beam.CanCollide = false
  40.                        
  41.                             local distance = (muzzle.CFrame.p - position).magnitude
  42.                             beam.Size = Vector3.new(0.15, 0.15, distance)
  43.                             beam.CFrame = CFrame.new(muzzle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
  44.                        
  45.                             local soundcopy = game.SoundService.Turret:Clone()
  46.                             soundcopy.Parent = workspace
  47.                             soundcopy:Play()
  48.                            
  49.                             debris:AddItem(soundcopy, soundcopy.TimeLength)
  50.                             debris:AddItem(beam, 0.01)
  51.                            
  52.                             lastShot = tick()
  53.                         end
  54.                     end
  55.                 end
  56.             end
  57.         end
  58.         wait()
  59.     end
  60. end
  61.  
  62. return turret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement