The following code is what I currently have in a server script within ServerScriptService. I have tried to finesse this code up and down, and cannot find a solution to save and load player's clothing when they leave and rejoin the game. Another thing I have noticed with this code, is that the game doesn't seem to read SetAsync or GetAsync, since no pcall function results are showing up in the output console. Please help, thank you very much!
local OutfitDataStore = game:GetService("DataStoreService"):GetDataStore("OutfitDataStore") local ServerStorage = game:GetService("ServerStorage") local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player) local character = player.Character or player.CharacterAdded:Wait()
local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate local pantsId = player.Character:WaitForChild("Pants").PantsTemplate local data local success, errormessage = pcall(function() data = OutfitDataStore:GetAsync(player.userId) end) if success then shirtId = data[1] pantsId = data[2] else print("No outfit to load from data store") 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() OutfitDataStore:SetAsync(player.userId,{shirtId, pantsId}) end) if success then print ("Player Data successfully saved") else print ("Error saving data") warn(errormessage) end
end)
You can't save objects to datastores, but you can do this. If you are saving a part, you can save if its anchored value or its color. Hope this helps!