How would I break a loop when a player doesn't own a sword without the output running an error

Hello developers! For the last 30 minutes, I have been trying to do this, and I’m thinking I would fire a remote event to all the clients and check if they have the sword or not, then break the loop if they don’t have the sword. Am I correct?

-- This is in a serverscript in serverscriptservice
for _, player in pairs(game.Players:GetChildren()) do
   if player.Backpack.AzureSword == true or player.StarterGear.AzureSword == true then
    player.Character.AzureSword:Destroy()
    player.Backpack.AzureSword:Destroy()
    player.StarterGear.AzureSword:Destroy()
  else
    break
end
end

Thanks, and have a great day!

Try using pcall, it basically checks through the script and if there’s an error it catches it but does not stop the rest of the script.

You can adjust your if statements to look for the swords in the backpack

if player.Backpack:FindFirstChild("AzureSword") or player.StarterGear:FindFirstChild("AzureSword") then
--put the rest of your code in here
end
1 Like

What would I put as one of the agurements?

If the player does not have a sword then the loop will just continue onto the next player? I think your getting confused on what break does. So in this case if the player does not have the sword it will stop the whole loop and continue with the code below it. Instead you would use continue to make the loop go onto the next key/value pair. But in this case the loop is going to automatically go through each player and if it does not have the sword it will just continue to the next player. And obviously listen to @DarkDanny04 to improve your code.

1 Like

Would it be better to use FireAllClients and then do that piece of code, so if one player doesn’t have the sword, only for them the loop would break?

The script should be done server side to prevent the client with tampering with it. It will not error if you properly use FindFirstChild.

I would suggest you to do what @DarkDanny04 said to do (And another thing is that you wrote Charcter and not Character. Another thing is that when your script fires, the character might not been added yet)