Hello,
I have a character customization system that is supposed to save and load the character’s chosen appearance using datastores, but it doesn’t work. I would like to have it saved once the player leaves the game, and loaded when the player joins, but the script is not even printing anything. I’m using a script I found in a similar topic. I would love any kind of help.
Script:
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")
local properties = {"HeadColor", "LeftArmColor", "LeftLegColor", "RightArmColor", "RightLegColor", "TorsoColor"}
local playersData = {}
local function serializeData(character)
local data = {}
local bodyColors = character["Body Colors"]
for _, property in ipairs(properties) do
data[property] = bodyColors[property].Name
end
local shirt = character:FindFirstChild("Shirt")
if shirt then
data["Shirt"] = shirt.ShirtTemplate
end
local pants = character:FindFirstChild("Pants")
if pants then
data["Pants"] = pants.PantsTemplate
end
local shirtGraphic = character:FindFirstChild("ShirtGraphic")
if shirtGraphic then
data["ShirtGraphic"] = shirtGraphic.Graphic
end
return data
end
local function deserializeData(character, data)
local bodyColors = character["Body Colors"]
for _, property in ipairs(properties) do
bodyColors[property] = BrickColor.new(data[property])
end
if data["Shirt"] then
local shirt = character:FindFirstChild("Shirt")
if shirt then
shirt.ShirtTemplate = data["Shirt"]
else
shirt = Instance.new("Shirt")
shirt.Parent = character
shirt.ShirtTemplate = data["Shirt"]
end
end
if data["Pants"] then
local pants = character:FindFirstChild("Pants")
if pants then
pants.PantsTemplate = data["Pants"]
else
pants = Instance.new("Pants")
pants.Parent = character
pants.PantsTemplate = data["Pants"]
end
end
if data["ShirtGraphic"] then
local shirtGraphic = character:FindFirstChild("ShirtGraphic")
if shirtGraphic then
shirtGraphic.Graphic = data["ShirtGraphic"]
else
shirtGraphic = Instance.new("ShirtGraphic")
shirtGraphic.Parent = character
shirtGraphic.Graphic = data["ShirtGraphic"]
end
end
end
local function onPlayerAdded(player)
local function onCharacterRemoving(character)
local function onPlayerRemoving(plr)
if player == plr then
local data = serializeData(character)
local success, result = pcall(function()
return datastore:SetAsync("BodyColors_"..player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
playersData[plr] = nil
end
players.PlayerRemoving:Connect(onPlayerRemoving)
end
local function onCharacterLoaded(character)
local data = playersData[player]
if data then
deserializeData(character, data)
end
end
player.CharacterRemoving:Connect(onCharacterRemoving)
player.CharacterAppearanceLoaded:Connect(onCharacterLoaded)
local success, result = pcall(function()
return datastore:GetAsync("BodyColors_"..player.UserId)
end)
if success then
if result then
playersData[player] = result
end
else
warn(result)
end
end
players.PlayerAdded:Connect(onPlayerAdded)