How do I make it so that if there is a shirt in the player it will follow the function but if not it will skip it

So I have a local script inside of the starter character scripts. I made a script to delete your shirt.

local shirt = script.Parent:WaitForChild("Shirt")
shirt:Destroy()

But how do I make it so that it does what this^ says if the player has a shirt and skips it if the player does not.

i’m going to ask for clarification, you want it to do shirt:destroy() only if a shirt is detected, and skip this action if there is no shirt detected?

1 Like

Try this!

local shirt = script.Parent:WaitForChild("Shirt")
if shirt then -- if a shirt is found
shirt:Destroy() -- destroys it
else -- if no shirt is found
end -- script ends
3 Likes

yes that is exactly what im asking for.

1 Like

for some sort of reason it does not work.

I think I know the reason. Because when it search for local shirt it can’t find it so it stops the script right away.

Now try it with a shirt just incase.

Your script is correct however there’s no reason to end with “)” that simply causes a syntax error, since you’re not closing correctly.
I’d suggest just removing the final point and it should be all good from there.

2 Likes
local _, Character = nil, script.Parent or script.AncestryChanged:Wait()
local Shirt = Character:FindFirstChildOfClass("Shirt")
if Shirt then
	print(Character.Name.." is wearing a shirt!")
	Shirt:Destroy()
end

This is working for me (local script inside StarterCharacterScripts).

Corrections have been made. Thank you!

To add to this, to ensure the shirt has loaded when the script runs, you can try waiting for the player appearance to load:

--script inside StarterCharacterScripts
local Character = script.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)

if not Player:HasAppearanceLoaded() then
	Player.CharacterAppearanceLoaded:Wait() 
end
local Shirt = Character:FindFirstChildWhichIsA("Shirt")

if Shirt then
	print("shirt found, and destroyed")
	Shirt:Destroy()
else
	print(Character.Name.." isn't wearing a shirt")
end
1 Like