This is the script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bullet = ReplicatedStorage:WaitForChild("Bullet")
local turret = script.Parent
local fireRate = 0.5
local bulletDamage = 10
local bulletSpeed = 150
local agroDist = 100
while wait(fireRate) do
--Find the target, detect if its realistic to shoot
local target = nil
for i, v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local hr = v:FindFirstChild("HumanoidRootPart")
if human and hr and human.Health > 0 then
if (hr.Position - turret.Position).magnitude < agroDist then
target = hr
end
end
end
if target then
local hr = target
--Turn the turret to face the target
turret.CFrame = CFrame.new(turret.Position, hr.Position)
local newBullet = Bullet:Clone()
newBullet.Position = turret.Position
newBullet.Parent = workspace
newBullet.Velocity = turret.CFrame.LookVector * bulletSpeed
newBullet.Touched:Connect(function(hit)
local human = hit:FindFirstChild("Humanoid")
if human.Health > 0 then
human:TakeDamage(10)
end
end)
end
end