Why doesnt this script work for a turret?

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

1 Like

When following the tutorial did you make sure to name and group all the items exactly the same so the script can identify each item?
Are you getting any errors in your Output Window?
You could try adding print statements in each section to see where the script isn’t working.

1 Like

You might want to be more clear on this. I can’t really help you if

  • the code doesnt look well formatted
  • And you didn’t tell much more info on how the game responds.

It’s like doing this

"Hey why does this dont work thanks

if i = 1 then
i = 20
elseif i=9 then
i = 1
elseif i = = 03
i= 220
elsie f i 0=
then i = 1

else = if i = 445
then e = 3
elseif i == 3 then
print(“fire”)

"

1 Like