How to save Color3 value in Datastore

local Players = game:GetService("Players") -- player.Parent
local Datastore = game:GetService("DataStoreService")

Datastore = Datastore:GetDataStore("colors")

-- other code

Players.PlayerRemoving:Connect(function(player)
	local ok, eror = pcall(function()
		Datastore:SetAsync((Color.R,Color.B,Color.G),player.info.color.Value)
	end)
	if not ok then
		print(eror) -- line that errors
	end
end)

Try this code

if you dont understand, i just made it so its

Datastore:SetAsync((Color.R,Color.B,Color.G),player.info.color.Value)

This will not at all work. Because your using the wrong parenthesis. Its suppose to be curly.

1 Like

ok

i tested and it works :smiley: :smiley: thanks

ofcourse i got something called synapse error but i added {}

but how do i get it?

What do you mean by how do i get it?

local Players = game:GetService("Players") -- player.Parent
local Datastore = game:GetService("DataStoreService")

Datastore = Datastore:GetDataStore("colors")

local function Serialize(color)
	local r,g,b = color.R,color.G,color.B
	return r..","..g..","..b
end

local function Deserialize(colorstring)
	local r,g,b = colorstring:split(",")[1],colorstring:split(",")[2],colorstring:split(",")[3]
	return Color3.new(tonumber(r),tonumber(g),tonumber(b))
end

Players.PlayerRemoving:Connect(function(player)
	local ok, eror = pcall(function()
		Datastore:SetAsync(pleyer.Name,Serialize(player.info.color.Value))
	end)
	if not ok then
		print(eror) -- line that errors
	end
end)

Players.PlayerAdded:Connect(function(player)
	local ok, eror = pcall(function()
		return Datastore:GetAsync(pleyer.Name)
	end)
	if ok then
		print(Deserialize(eror))
	end
end)

I wrote it for you I suppose.

i mean how do i GetAsync on it

how do i know what the color is

i get attempt to index nil with split or something

I have thought that the string of the Value may not be validated by ROBLOX standard.

Basically means your not giving it the argument of colorstring.

Does the code in my reply not work, @SomeFedoraGuy? Also,

In this case ā€œerorā€ would be nil since ā€œokā€ is true.

That would be the key, you would want to use the Playerā€™s ID when saving it.

but i am, look:
image

Errr, I canā€™t really test anything because A: I am busy and B: I am lazy. But:

Players.PlayerAdded:Connect(function(player)
	local ok, eror = pcall(function()
		return Datastore:GetAsync(player.Name)
	end)
	if ok then
		print(Deserialize(eror))
	end
end)

You should check if eror is nil though:

Players.PlayerAdded:Connect(function(player)
	local ok, eror = pcall(function()
		return Datastore:GetAsync(player.Name)
	end)
	if ok and eror then
		print(Deserialize(eror))
	end
end)

Youā€™re looking for the term ā€˜Serializationā€™.

Hereā€™s the most efficient way of converting a Color3 data type to table format:

local function serializeColor(color: Color3)
	local r, g, b = color.R, color.G, color.B
	r, g, b = math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)
	
	return {r, g, b}
end

local function deserializeColor(serialzedColor: any)
	local r, g, b = serialzedColor[1], serialzedColor[2], serialzedColor[3]
	
	return Color3.fromRGB(r, g, b)
end

local serialized = serializeColor(COLOR)
local deserialized = deserializeColor(serialized)

cant i use Color3.fromrgb like that somefedoraguy said?
look:

Hereā€™s ur finished code!


local Players = game:GetService("Players") -- player.Parent
local Datastore = game:GetService("DataStoreService")
Datastore = Datastore:GetDataStore("colors")

-- other code

Players.PlayerRemoving:Connect(function(Player)
	local Color = Color3.new(1,1,1)
	local ok, eror = pcall(function()
		Datastore:SetAsync(Player.UserId,Color.R.."/"..Color.G.."/"..Color.B)
	end)
	if not ok then
		print(eror) -- line that errors
	end
end)
Players.PlayerAdded:Connect(function(Player)
	local Return = Datastore:GetAsync(Player.UserId)
	if type(Return) == "string" then
		local ColorReturned = {}
		for i,v in pairs(string.split(Return,"/")) do
			if i == 1 then
				ColorReturned["R"] = v
			end
			if i == 2 then
				ColorReturned["G"] = v
			end
			if i == 3 then
				ColorReturned["B"] = v
			end
		end
		local ColorReturnedComplete = Color3.new(ColorReturned["R"],ColorReturned["G"],ColorReturned["B"])
	else
		print("No Saves")
	end
end)

Edit: I Fixed a typo

I was going to do that but had to pause because of irl work.

What kind of human are youā€¦ please stay a mile away from me, from now on. As:

local Players = game:GetService("Players") -- player.Parent
local Datastore = game:GetService("DataStoreService")
Datastore = Datastore:GetDataStore("colors")

-- other code

Players.PlayerRemoving:Connect(function(Player)
	local Color = Color3.new(1,1,1)
	local ok, eror = pcall(function()
		Datastore:SetAsync(Player.UserId,Color.R.."/"..Color.G.."/"..Color.B)
	end)
	if not ok then
		print(eror) -- line that errors
	end
end)
Players.PlayerAdded:Connect(function(Player)
	local Return = Datastore:GetAsync(Player.UserId)
	if type(Return) == "string" then
		local ColorReturned = string.split(Return,"/")
-- could be like this ngl
	else
		print("No Saves")
	end
end)

Why do you insult me when i litterary tried to help