Unable to delete shirt/pants

I’m trying to delete the player’s classic shirt/pants but it’s not working. Here’s the code:

	-- Assign Clothing: Shirt
	local shirt = char:FindFirstChild("Shirt")
	if shirt ~= nil then
		shirt.Parent = nil
		shirt:Destroy()
	end
	shirt = Instance.new("Shirt")
	shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=" .. tostring(data.shirt.assetid)
	shirt.Color3 = data.shirt.color.Color
	shirt.Name = "Shirt"
	shirt.Parent = char
	
	-- Assign Clothing: Pants
	local pants = char:FindFirstChild("Pants")
	if pants ~= nil then
		pants.Parent = nil
		pants:Destroy()
	end
	pants = Instance.new("Pants")
	pants.PantsTemplate = "http://www.roblox.com/asset/?id=" .. tostring(data.pants.assetid)
	pants.Color3 = data.pants.color.Color
	pants.Name = "Pants"
	pants.Parent = char

The asset IDs are correct, and the new items are created, but the old ones are still there. Any ideas as to what’s going on?

local char = script.Parent
local shirt = char:WaitForChild("Shirt")
if shirt then
	shirt:Destroy()
	print("shirt destroyed :o")
end

local shirts = Instance.new("Shirt")
shirts.ShirtTemplate = "http://www.roblox.com/asset/?id=256120924" -- example, no need to use this
shirts.Color3 = Color3.new(1, 1, 1) -- example, no need to use this
shirts.Name = "Shirtss"
shirts.Parent = char

local pants = char:WaitForChild("Pants")
if pants then
	pants:Destroy()
	print("pants destroyed :o")
end

local pants = Instance.new("Pants")
pants.PantsTemplate = "http://www.roblox.com/asset/?id=256120924" -- example, no need to use this
pants.Color3 = Color3.new(1, 1, 1) -- example, no need to use this. 
pants.Name = "Shirtss"
pants.Parent = char

Put this on startercharacterscript

instead of destroying and making a new one:

	local shirt = char:FindFirstChildOfClass("Shirt")
	shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=" .. tostring(data.shirt.assetid)
	shirt.Color3 = data.shirt.color.Color
	shirt.Name = "Shirt"
	shirt.Parent = char
	
	-- Assign Clothing: Pants
	local pants = char:FindFirstChildOfClass("Pants")
	pants.PantsTemplate = "http://www.roblox.com/asset/?id=" .. tostring(data.pants.assetid)
	pants.Color3 = data.pants.color.Color
	pants.Name = "Pants"
	pants.Parent = char

@war44malk @p49p0
It seems that the root of the problem is that FindFirstChild is returning nil for Shirt and Pants, even though I can see them in Studio.

Use FindFirstChildOfClass if the shirts/pants has a different name

local shirt = char:FindFirstChildOfClass("Shirt")
local pants = char:FindFirstChildOfClass("Pants")

They don’t have different names. It actually shows in studio “Shirt” and “Pants” as children of the player’s character model.

FindFirstChild will return nil because it didn’t load yet. Always use WaitForChild() when defining variables at the top of the script. You can use FindFirstChild in events and functions because it’s already loaded when you get to the event or call a function.

Yeah. I figured that’s what was going on. It seems that when Player.CharacterAdded fires, the player’s character model hasn’t fully loaded yet. I ended up spawning a new thread with a delay loop so it will wait for the items to load. It will eventually find it. Here’s the new code in case someone needs to do something in the future. This doesn’t delete the shirt/pants, but modifies it per @p49p0’s suggestion and which I was attempting to do in the first place before I tried to delete them outright.

	-- Assign Clothing: Shirt
	task.spawn(function()
		local shirt = char:FindFirstChild("Shirt")
		while shirt == nil do
			task.wait(0.1)
			shirt = char:FindFirstChild("Shirt")
		end
		if shirt ~= nil then
			shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=" .. tostring(data.shirt.assetid)
			shirt.Color3 = data.shirt.color.Color
			print("Shirt Modified")
		end

	end)
	
	-- Assign Clothing: Pants
	task.spawn(function()
		local pants = char:FindFirstChild("Pants")
		while pants == nil do
			task.wait(0.1)
			pants = char:FindFirstChild("Pants")
		end
		if pants ~= nil then
			pants.PantsTemplate = "http://www.roblox.com/asset/?id=" .. tostring(data.pants.assetid)
			pants.Color3 = data.pants.color.Color
			print("Pants Modified")
		end
	end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.