The issue is when this script is run after one target is destoryed it never goes to the the next person
BulletSpeed = 200
CurrentTarget = nil
MaxDistanceForShooting = 50
while true do
for i,v in pairs(game.Workspace:GetChildren()) do
local humanoid = v:FindFirstChild(“Humanoid”)
local torso = v:FindFirstChild(“Torso”)
if humanoid and CurrentTarget == nil and humanoid.Health ~= 0 and (torso.Position - script.Parent.Position).Magnitude < MaxDistanceForShooting then
print(“firstarget fount”)
CurrentTarget = torso
script.Parent.CFrame = CFrame.new(script.Parent.Position, CurrentTarget.Position)
ShootingPart = game.ServerStorage.ShootingPart:Clone()
ShootingPart.Parent = game.Workspace
ShootingPart.Position = script.Parent.Position
ShootingPart.Velocity = script.Parent.CFrame.LookVector * BulletSpeed
ShootingPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
hit.Parent:FindFirstChild(“Humanoid”):TakeDamage(10)
print(“shot”)
end
end)
elseif CurrentTarget ~= nil and humanoid.Health ~= 0 and (torso.Position - script.Parent.Position).Magnitude < MaxDistanceForShooting then
print(“luanching repeated attack on target”)
while true do
if CurrentTarget.Parent:FindFirstChild(“Humanoid”).Health == 0 then
print(“work”)
CurrentTarget = nil
end
script.Parent.CFrame = CFrame.new(script.Parent.Position, CurrentTarget.Position)
ShootingPart = game.ServerStorage.ShootingPart:Clone()
ShootingPart.Parent = game.Workspace
ShootingPart.Position = script.Parent.Position
ShootingPart.Velocity = script.Parent.CFrame.LookVector * BulletSpeed
ShootingPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
hit.Parent:FindFirstChild(“Humanoid”):TakeDamage(10)
else
print(“Did not touch the target”)
end
end)
wait(1)
end
end
end
wait(1)
end