-
What do you want to achieve?
I’m trying to make an RP game and I’m finding it hard for me to save the players current avatar with my custom avatar editor. I also need some insight on how I toggle accessories/get all current accessories -
What is the issue?
I can’t figure out how I would save the player’s avatar to a datastore. -
What solutions have you tried so far?
I’ve looked a lot for a solution, but can’t find much.
Here’s my current code for the system
local dss = game:GetService("DataStoreService")
local datastore = dss:GetDataStore("PlayerData")
local players = game.Players
local data = {}
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Money.Value; -- First value from the table
player.leaderstats.Coins.Value -- Second value from the table
}
local success, err = pcall(function()
datastore:SetAsync(player.UserId, data[player.Name]) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
players.PlayerAdded:Connect(function(player)
local err, success = pcall(function()
data[player.Name] = datastore:GetAsync(player.UserId)
end)
player.CharacterAdded:Connect(function(character)
if #data ~= 0 and data[player.Name] ~= nil then
local face = data[player.Name]["Avatar"]["Face"]
if face then
character.Head.face.Texture = face
end
end
end)
end)
players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
event.OnServerEvent:Connect(function(player,type,content)
local character = player.Character
if type == "Face" then
character.Head.face.Texture = "rbxassetid://"..content
data[player.Name]["Avatar"]["Face"] = "rbxassetid://"..content
end
end)