Attempt to index nil with 'Shirt'

I would like the current clothing (of a player leaving the game) to be saved, but I always get the error code ,attempt to index nil with ‘Shirt’ ,. What does that mean ? What have to be fixed ?

local DataStoreService = game:GetService("DataStoreService")
local ClothingStore = DataStoreService:GetDataStore("ShirtStore")




game.Players.PlayerAdded:Connect(function(player)
	
	
	
	

	local success, ShirtID = pcall(function()
	
		ClothingStore:GetAsync(player.UserId)
	
	end)

	if success then 
		print(ShirtID)
	end
	
	game.Players.PlayerRemoving:Connect(function()
		
		
		local success, errorMessage = pcall(function()

			ClothingStore:SetAsync(player.UserId, player.Character.Shirt.ShirtTemplate)

		end)

		if success then 
			print("ShirtID saved!")
		else 
			print(errorMessage) 
		end
	
		
		
		
		
		
		
	end)
	
	
	
	
	
end)


	
	
	

when u create a shirt instance roblox studio renames it to “Clothing” so try:

ClothingStore:SetAsync(player.UserId, player.Character.Clothing.ShirtTemplate)

It says Clothing is not a valid member of Model "Workspace.Noay_HD1"

maybe try this

local shirt = player.Character:FindFirstChildOfClass("Shirt")
ClothingStore:SetAsync(player.UserId, shirt.ShirtTemplate)

Doesnt work (attempt to index nil with 'FindFirstChildOfClass' ).

Try adding player.CharacterAdded:Wait() before it.

game.Players.PlayerRemoving:Connect(function(player)
    local character = player.Character
    if character then
        local success, errorMessage = pcall(function()
            ClothingStore:SetAsync(player.UserId, character.Shirt.ShirtTemplate)
        end)
        if success then 
            print("ShirtID saved!")
        else 
            print(errorMessage) 
        end
    end
end)

I think this would help :sweat_smile:

it’s cause you’re not checking if the character actually exists, you need to verify that.

The player can also have no shirt in their character. Which is possible to do, by just not equipping a shirt.

So you should do something like this instead

game.Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(Character)
		if Character:FindFirstChildOfClass("Shirt") then

			-- Your code goes here.

		else

			-- Maybe an error message, this is optional.

		end
	end)
	
end)

What if the character didn’t load? So that’s why you should probably use CharacterAdded

I assume the character is gone before the event is fired you could probably save the shirt id as a variable during player.CharacterRemoving (RBXScriptSignal).