Help my script isnt working the target part

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

Bit hard to see what’s going on, but does the script break when a target is destroyed?

yess ur right thats what happens

Try putting some pcalls in you script where it requires the target, see if that works

Use HumanoidRootPart.

Torso is for like R6. R15 is like UpperTorso. So rather than try to work out which is which, use HumanoidRootPart.

You really should put in “print()” in there with the value you want to check. Check each point.

local humanoid = v:FindFirstChild(“Humanoid”)
print("humanoid1")
print(humanoid)
local torso = v:FindFirstChild(“Torso”)
print("torso1")
print(torso)

And when posting code, highlight it all and click “</>”

That will format it so we can read it better.

If you’re not sure how to use codeblcks, this would help you

Please format your code properly and make an attempt to highlight where exactly your issue is going wrong. This category is not a do-my-work category; refrain from posting threads saying your code doesn’t work and then dumping an entire script into the thread.

yes but the checking works since om using r6 for testing

Nevermind figured it out I made a better script

1 Like