Hey I'm a new scripter and I'd appreciate any help. Giving some background, the player's clothes change in server. I'm trying to make a script that when the player leaves, it saves those clothes and when they return they have those same clothes. Trying to figure out datastore, but struggling quite a bit.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate
local pantsId = player.Character:WaitForChild("Pants").PantsTemplate
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId, {shirtId, pantsId})
end)
if success then
shirtId = data[1]
pantsId = data[2]
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate
local pantsId = player.Character:WaitForChild("Pants").PantsTemplate
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId,{shirtId, pantsId})
end)
if success then
print ("Player Data successfully saved")
else
print ("Error saving data")
warn(errormessage)
end
end)

4 comments
Also I get the error: ServerScriptService.clothesData:7: attempt to index nil with 'WaitForChild'
I believe that WaitForChild error is due to the character not fully loading in before the script does, that's just a guess. But also I think it does save the clothes if you don't get that one error, all you have to do is apply the id to either a pants object or a shirt object.
I'm getting attempt to index nil with number. At line 14
Just in case I didn't explain what I'm trying to do properly. Players clothes change in-game. I want to save it and have it load next time they join the game.