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