Saving a player's current custom (game-only) avatar

  1. 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

  2. What is the issue?
    I can’t figure out how I would save the player’s avatar to a datastore.

  3. 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)

1 Like

Why not have a dictionary storing the strings of each item the player is currently wearing.

local Data = {}

Data[Player.UserId] = {
       Weapons = {}; 
       Avatar = {
             Hats = {"StrawHat"};
             Accessories = {"Steel Gloves", "Cool Robe"};
       -- add whatever else
       }
       Stats = {
             Level = 1;
       };

};

When you want to give the player or edit their items just loop through the table you want and do what you need to with those items.

Sorry for the dull reply and bad indents, currently on mobile so kind of hard to explain further.

1 Like

the indentation is fine, don’t worry about that

Currently I only have faces working, but not a bad idea! Right now in the modulescript where I’m containing the face ids though I ONLY provide the Id. I could change that though. The reason why I want to set it up that way is so I don’t have to write a table EVERY SINGLE TIME I want to add an item to the editor