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
@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.
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)