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