Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make the script keep going even if the thing it's searching for doesn't exist?

I'm trying to make a game where once the player joins, they look like a noob but if the player isn't wearing any clothing, the script doesn't work. Any help?

This is the script

game.Players.PlayerAdded:Connect(function(player)
	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
end)

1 answer

You could run a check to see if they have a shirt or pants and then delete them.

game.Players.PlayerAdded:Connect(function(player)
	repeat wait() until player.Character
	if player.Character:FindFirstChild("Shirt") then
		player.Character.Shirt:Destroy()
	end
	if player.Character:FindFirstChild("Pants") then
		player.Character.Pants:Destroy()
	end
end)

Or, you could just pcall the Destroy method. This isn't exactly considered good practice, though.

game.Players.PlayerAdded:Connect(function(player)
	repeat wait() until player.Character
	pcall(function()
		player.Character.Shirt:Destroy()
	end)
	pcall(function()
		player.Character.Pants:Destroy()
	end)
end)

1 comment

0

16:49:32.011 - Infinite yield possible on 'DaBrainlessOne:WaitForChild("Shirt")'

This discussion is preserved as it was. New answers, comments, and votes are closed.