I followed this tutorial and my turret doesn’t work I don’t know why
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Bullet = ReplicatedStorage:WaitForChild(“Bullet”)
local turret = game.Workspace.Turret.FiringPart.Parent.Union.Parent.part
local FIRERATE = 0.1
local BULLETDAMAGE = 5
local BULLETSPEED = 250
local ATTACKDISTANCE = 150
while wait(FIRERATE) do
– Find the target, Detect if its 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 < ATTACKDISTANCE then
local BulletRay = Ray.new(turret.Position, (torso.Position - turret.Position).Unit * ATTACKDISTANCE)
local hit, Position = game.Workspace:FindPartsInRegion3WithIgnoreList(BulletRay, {turret})
if hit == torso then
else
print(“Object in the way”)
end
target = torso
end
end
end
if target then
local torso = target
–Turn the turret to face the 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