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)
You made a few mistakes with the datastore: first of all, getAsync has only one paramter and thats the key, secondly you need a default value incase you haven't stored a value yet for this person in that case it will return a nil so when u get a nil u give the default value
local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(player.UserId ) end)
Next like the person said in the comments you need to wait for the character to exist first before you ask its children.
player.CharacterAdded:wait()