How can I store a color3 in :FireServer() and then use the color3 in :SetAsync() and :GetAsync()

Im trying to make it so when a player touches a button it saves the data. But aprarently you cant save data in local scripts so I have a remote function to save and load the data. But how can I save color3 Values?

Activation:

local BodyColor = game.ReplicatedStorage.CharacterColors.Red["Body Colors"].HeadColor3
local PlrID = game.Players.LocalPlayer.UserId
local Char = game.Players.LocalPlayer.Character
local NewBodyColor = {BodyColor.R,BodyColor.B,BodyColor.G}
local ColorSaver = game.ReplicatedStorage.SaveColors
local ColorLoader = game.ReplicatedStorage.LoadColors


script.Parent.MouseButton1Click:Connect(function()
	ColorSaver:FireServer(PlrID, NewBodyColor)
	ColorLoader:FireServer(PlrID, NewBodyColor)
end)

Save:

local DataStore = game:GetService("DataStoreService")
local Data = DataStore:GetDataStore("PlayerColor")

game.ReplicatedStorage.LoadColors.OnServerEvent:Connect(function(ID, Color)
	Data:SetAsync(ID, Color[1],Color[2],Color[3])
end)

Load:

local LoadEvent = game.ReplicatedStorage.LoadColors
local Char = script.Parent
wait(0.01)
local HRP = Char:FindFirstChildWhichIsA("Humanoid")
local PlrColors = Char:FindFirstChildWhichIsA("BodyColors")
local Data = game:GetService("DataStoreService"):GetDataStore("PlayerColor")
local PlayerIdFetcher = game.ReplicatedStorage.PlayerID
local PlrID

PlayerIdFetcher.OnServerEvent:Connect(function(plr) --- Getting the playerID
	PlrID = plr
end)


local success, color = pcall(function()
	return Data:GetAsync(PlrID) or Color3.fromRGB(248, 248, 248)
end)
if success then
	PlrColors.HeadColor3 = color
--    ...
end


LoadEvent.OnServerEvent:Connect(function(PlrID, Color)
	PlrColors.HeadColor3 = color
--    ...
end)

Save the color as a string, then when loading the data from the database, you can use string.split(β€œ,”) and load into Color3.fromRGB().

2 Likes

Convert the Color from RGB to Hexadecimal (or Hexcode), it is simple to do, and will always be the exact same size (which is six characters) unlike other formats which can take up to 9 characters, which saves space.

I guess it works its just that when im changing the players bodycolor I need to convert to rgb which I cant figure out.

I am not really sure, but you can do this:
function RGBMath(number)
return 255 * number
end
local Color = Color3.new(100,100,100)
local rgb = Color3.fromRGB(RGBMath(Color.R),RGBMath(Color.G),RGBMath(Color.B))

Note: I made the calculation of my own. It might not work

Can you please explain how to use it because im confused

Instructions Included Script:

function RGBMath(number) – Function
return 255 * number – Math to covert number to rgb. Example, Red is 0.7 and it will make it rgb value
end
local Color = Color3.new(100,100,100) – Insert the color3 value you need to convert to rgb
local rgb = Color3.fromRGB(RGBMath(Color.R),RGBMath(Color.G),RGBMath(Color.B)) – It makes color to rgb

Will it work with hex code? I forgot to say that im trying to convert hex

No, it works with color3 to RGB.

can you make the script work with hex to rgb?

function rgbToHex(rgbgiven)
local rgb = {rgbGiven.R,rgbGiven.G,rgbGiven.B)
local hexadecimal = β€˜0X’

for key, value in pairs(rgb) do
	local hex = ''

	if value > 0 do
		local index = math.fmod(value, 16) + 1
		value = math.floor(value / 16)
		hex = string.sub('0123456789ABCDEF', index, index) .. hex			
	end

	if string.len(hex) == 0 then
		hex = '00'

	elseif string.len(hex) == 1 then
		hex = '0' .. hex
	end

	hexadecimal = hexadecimal .. hex
end

return hexadecimal

end

I believe you can’t save the color because the first parameter is always the player so therefore, your ID is turning into player and color is turning into the ID

game.ReplicatedStorage.LoadColors.OnServerEvent:Connect(function(player, ID, Color)
	Data:SetAsync(ID, Color[1],Color[2],Color[3])
end)

My suggestion is you also remove the ID part because you can just get it from the player and exploiters could spoof it and probably break something

game.ReplicatedStorage.LoadColors.OnServerEvent:Connect(function(player, Color)
	Data:SetAsync(player.UserId, Color[1],Color[2],Color[3])
end)

I added R, B,G in loaded and saving scripts.

Color3.fromHex(hexcode) -- imsert the converted hexcode to convery to RGB

RGBColor:ToHex() -- converts to Hex

Its very simple.

Saving it as a table takes up more data, its very inefficient, and I would not recommend it at all, you would have a better time just converting it to hexcode, and then converting it back to rgb.

When saving it as a table, you are essentially telling the key to remember 4 pieces of data, 1 of them being the table to store data, and the other 3 being the rgb values, very inefficient, while with the hexcode, you are only saving one value that will always be the same, instead of 4 values. Much better.

:ToHex() will convert the color into a string indicating its hexcode, which is 6 characters long, Color3.fromHex will convert that string to a rgb value.

its easy to convert to hex. but not easy to convert back to rgb

Its simple, all you are doing is convertting between base 10 and base 16 (hexadecimal, which is hexcode), and vice versa, there isnt much to it

fromHex will do the work for you, as well as :ToHex

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.