Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a script continue working even if it can't find the thing it's searching for?

Asked by 6 years ago
Edited 6 years ago

I'm trying to code a thing where it would remove the clothes of the player, and re-color them to look like a noob but the script will cease to run if the player isn't wearing any clothes. How could I get the script to keep running even if something isn't found? (Here's my attempt)

 game.Players.PlayerAdded:Connect(function(player)
     player.CharacterAdded:Wait()
    if player.Character.Shirt == nil then
        print("There is no need to delete the shirt")
    else
        player.Character.Shirt:Destroy()
    end
    player.Character:WaitForChild("Pants")
    if player.Character.Pants == nil then
        print("There is no need to delete the pants")
    else
        player.Character.Pants:Destroy()        
    end
local d = player.Character:GetChildren()    
    for i=1, #d do 
        if (d[i].className == "Accessory") then 
            d[i]:remove() 
        end 
    end
end)        -- There's stuff in between and after this code, but I just put the points of interest.


Answer this question