Here is my code:
local RS = game:GetService("ReplicatedStorage")
local Bullet = RS:WaitForChild("Bullet")
local Turret = script.Parent
local fireRate = 0.5
local BulletDamage = 10
local BulletSpeed = 500
local agroDist = 100
while wait(fireRate) do
--Find the target, detect if realistic to shoot
local target = nil
for i,v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local Torso = v:FindFirstChild("Torso")
if human and Torso and human.Health > 0 then
if (Torso.Position - Turret.Position).Magnitude < agroDist then
target = Torso
end
end
end
if target then
--turn the turret to target
local Torso = target
Turret.CFrame = CFrame.new(Turret.Position, Torso.Position )
local newBullet = Bullet:Clone()
newBullet.Position = Turret.Position
newBullet.Parent = game.Workspace
newBullet.Velocity = Turret.CFrame.LookVector + BulletSpeed
newBullet.Touched:Connect(function(objectHit)
local human = objectHit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(BulletDamage)
end
end)
end
end
here is the error:
Any help is appreciated