Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local turret = script.Parent
- turret.PrimaryPart = turret:WaitForChild("RotationPart")
- local cooldown = 1
- local cooldownActive = false
- local maxRange = 50
- local dmg = 5
- while wait() do
- local closestCharacter = nil
- for i, plr in pairs(game.Players:GetPlayers()) do
- if plr.Character then
- local distance = (plr.Character.HumanoidRootPart.Position - turret.PrimaryPart.Position).Magnitude
- if not closestCharacter or (closestCharacter.HumanoidRootPart.Position - turret.PrimaryPart.Position).Magnitude > distance then
- local cf = CFrame.new(turret.PrimaryPart.Position, plr.Character.HumanoidRootPart.Position)
- local ray = Ray.new(turret.PrimaryPart.Position, cf.LookVector * maxRange)
- local part, position = workspace:FindPartOnRayWithIgnoreList(ray, turret.Parent:GetDescendants())
- if part then
- local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
- if humanoid == plr.Character.Humanoid and distance <= maxRange and humanoid.Health > 0 then
- closestCharacter = plr.Character
- end
- end
- end
- end
- end
- if closestCharacter then
- turret:SetPrimaryPartCFrame(turret.PrimaryPart.CFrame:Lerp(CFrame.new(turret.PrimaryPart.Position, closestCharacter.HumanoidRootPart.Position), 0.3))
- local humanoid = closestCharacter:FindFirstChild("Humanoid")
- spawn(function()
- if humanoid and not cooldownActive then
- cooldownActive = true
- humanoid:TakeDamage(dmg)
- turret.ShootPart.ShootSound:Play()
- local bullet = Instance.new("Part")
- local distance = (turret.ShootPart.Position - closestCharacter.HumanoidRootPart.Position).Magnitude
- bullet.CFrame = CFrame.new(turret.ShootPart.Position, closestCharacter.HumanoidRootPart.Position)
- bullet.CFrame = bullet.CFrame + (bullet.CFrame.LookVector * distance / 2)
- bullet.Size = Vector3.new(0.05, 0.05, distance)
- bullet.CanCollide = false
- bullet.Anchored = true
- bullet.Color = Color3.fromRGB(239, 184, 56)
- bullet.Parent = workspace
- game.Debris:AddItem(bullet, 0.1)
- wait(cooldown)
- cooldownActive = false
- end
- end)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement