I was trying to make a Sentry Turret that can damage other players and other turrets, but I saw that the for loop wasn't actually looping. By not-looping I mean that it might not be checking all the children. This is the script.
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local claimed = script.Parent:WaitForChild("Claimed")
local turretModel = script.Parent
local turret = turretModel:WaitForChild("TurretPart")
local turretSound = turret:WaitForChild("TurretFire")
local startingCFrame = turret.CFrame
local bulletSpeed = 500
RunService.Stepped:Connect(function()
if claimed.Value then
local playerWhoClaimed = script.Parent:WaitForChild("PersonClaimed").Value
local target
for _, v in pairs(workspace:GetDescendants()) do
local gotTurret = v:FindFirstChild("TurretPart")
local humanoid = v:FindFirstChildOfClass("Humanoid")
local head = v:FindFirstChild("Head")
if humanoid and head and v.Name ~= playerWhoClaimed and v ~= turretModel then
if humanoid.Health >= 1 then
if gotTurret then
local claimedValue = gotTurret.Parent:WaitForChild("Claimed")
if claimedValue.Value then
target = head
print(target)
end
else
target = head
print(target)
end
end
end
end
if target then
turretSound:Play()
turret.CFrame = CFrame.new(turret.Position, target.Position)
local bullet = turretModel:WaitForChild("Bullets"):WaitForChild("ExampleBullet"):Clone()
bullet.Anchored = false
bullet.Name = "Bullet"
bullet.CFrame = turret.CFrame
bullet.Transparency = 0
bullet.Parent = turretModel:WaitForChild("Bullets")
bullet:WaitForChild("BodyVelocity").Velocity = turret.CFrame.LookVector * bulletSpeed
local debounce = false
bullet.Touched:Connect(function(hit)
if hit and hit.Parent then
local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
local gotTurret = hit.Parent:FindFirstChild("TurretPart")
if humanoid and not debounce and hit.Parent ~= turretModel and hit.Parent.Name ~= playerWhoClaimed and hit.Name ~= "Bullet" then
humanoid.Health = 0
for _, v in pairs(hit.Parent:GetChildren()) do
if v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") or v:IsA("TriangleMeshPart") then
v.Anchored = false
end
end
if gotTurret then
gotTurret.Parent.Humanoid.Health = 0
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 8
explosion.Position = gotTurret.Parent.BottomPart1.Position
explosion.Parent = gotTurret.Parent.BottomPart1
gotTurret.Parent.Claimed.Value = false
end
hit.Parent:BreakJoints()
bullet:Destroy()
elseif hit.Parent.Parent:FindFirstChildOfClass("Humanoid") and not debounce and hit.Parent.Parent.Name ~= playerWhoClaimed and hit.Name ~= "Bullet" then
local turret2 = hit.Parent.Parent:FindFirstChild("TurretPart")
hit.Parent.Parent:FindFirstChildOfClass("Humanoid").Health = 0
for _, v in pairs(hit.Parent.Parent:GetChildren()) do
if v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") or v:IsA("TriangleMeshPart") then
v.Anchored = false
end
end
if turret2 then
turret2.Parent.Humanoid.Health = 0
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 8
explosion.Position = turret2.Parent.BottomPart1
explosion.Parent = turret2.Parent.BottomPart1
turret2.Parent.Claimed.Value = false
end
hit.Parent.Parent:BreakJoints()
bullet:Destroy()
end
else
warn("No parent found.")
end
end)
else
turret.CFrame = startingCFrame
end
end
end)

1 comment
Nevermind, I fixed it. The problem must've been a Studio bug.