Saving Players Body Colours

Hello,
I am making a script that data saves a players body colour, but I am unsure how to actually save and load the colour. I have this data store with everything made, but unsure how to save/load the body colours. I have a custom startercharacter and I would like to use this to save the colours for it, like a shirt that utilizes body colours. Any help would be appreciated, couldn’t find anything to help.

Script:

local DSS = game:GetService("DataStoreService")
local ColourStore = DSS:GetDataStore("DataColors")
local DefaultColour = nil
game.Players.PlayerAdded:Connect(function(Player)
	local data
	local success, result = pcall(function()

		data = ColourStore:GetAsync(Player.UserId)
		
	end)
	if success then
		if data then
			
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(player)	
	local success, result = pcall(function()
		ColourStore:SetAsync("Player_"..player.UserId)
	end)

	if success then
		print(result)
	else
		warn(result)
	end
end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local success, result = pcall(function()
		   ColourStore:SetAsync("Player_"..player.UserId, player.Quest.Value)
		end)

		if success then
			print(result)
		else
			warn(result)
		end
	end
end)
1 Like
local datastores = game:GetService("DataStoreService")
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
	return data
end

local function deserializeData(character, data)
	local bodyColors = character["Body Colors"]
	for propertyName, propertyValue in pairs(data) do
		bodyColors[propertyName] = BrickColor.new(propertyValue)
	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
		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)

I did test this & it works.

1 Like

Ah, thanks, is there also a way to do this with the players shirt?

local datastores = game:GetService("DataStoreService")
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)

Yes, I’ve covered all bases and added support for pants/tshirt too.

3 Likes

Alright, I’ll test it tomorrow and see if it works!

I think it’s possible to save Color3 and BrickColor values (You can create them as instances), if you can’t with DataStore, then save 3 Values for R,G,B to the player’s name.

Thanks a lot man, it works great! I will be able to do a lot now.

Also one more quick question, on the line where it says

else 
shirt = instance.new("Shirt")
shirt.Parent = character
shirt.ShirtTemplate = data["Shirt"]

Am I able to switch this out with a line that gives them a shirt if they don’t have any data?

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
else
	local shirt = character:FindFirstChild("Shirt")
	if shirt then
		shirt.ShirtTemplate = "rbxassetid://" --change this
	else
		shirt = Instance.new("Shirt")
		shirt.Parent = character
		shirt.ShirtTemplate = "rbxassetid://" --change this
	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
else
	local pants = character:FindFirstChild("Pants")
	if pants then
		pants.PantsTemplate = "rbxassetid://" --change this
	else
		pants = Instance.new("Pants")
		pants.Parent = character
		pants.PantsTemplate = "rbxassetid://" --change this
	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
else
	local shirtGraphic = character:FindFirstChild("ShirtGraphic")
	if shirtGraphic then
		shirtGraphic.Graphic = "rbxassetid://" --change this
	else
		shirtGraphic = Instance.new("ShirtGraphic")
		shirtGraphic.Parent = character
		shirtGraphic.Graphic = "rbxassetid://" --change this
	end
end

Alright thanks! I appreciate you always being there answering my questions lol