How to save Color3Value

Hi, I started making a game and ran into a problem in the first hour of work)
I don’t know how to save color3
I’ve already tried a lot of things, but all in vain, since there is not much knowledge

Leaderstats and DataStore

local DS = game:GetService("DataStoreService")
local DSS = DS:GetDataStore("GameTest1")


game.Players.PlayerAdded:Connect(function(plr)
	local FKatas = Instance.new("Folder", plr)
	FKatas.Name = "MainFolder" 
	
	local Kata = Instance.new("IntValue",FKatas)
	Kata.Name = "Kata"
	Kata.Value = "nil"
	
	local KatCSave = Instance.new("StringValue", FKatas)
	KatCSave.Name = "KatanaColorSave"
	KatCSave.Value = "255,255,55"
	
	local KatanaC = Instance.new("Color3Value",FKatas)
	KatanaC.Name = "KatanaColor"
	KatanaC.Value = Color3.new(255,255,255)
	
	local KatanaF = Instance.new("Folder",plr)
	KatanaF.Name = "KatanaFolder"
	
	local color = KatanaC.Value
	local katana = KatanaF.Tool.BladeKata
	
	local data
	local succe,er = pcall(function()
		data = DSS:GetAsync(plr.UserId)
	end)
	
	if succe then
		Kata.Value = data
		color.R = data
		color.G = data
		color.B = data
		
	else
		warn(er)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local succ, err = pcall(function()
		local data = DSS:SetAsync(plr.UserId, plr.MainFolder.Kata.Value, plr.MainFolder.KatanaColor.Value.R,plr.MainFolder.KatanaColor.Value.G,plr.MainFolder.KatanaColor.Value.B)
		
	end)
		if err then
			warn(err)
end

	game:BindToClose(function()
		for i,plr in pairs(game.Players:GetChildren()) do
			
		end
	end)
end)

I think this may help.

Yea just saw the script, you need to make the Save Value color3 as well i think

And use DataStores to save the Values and reload

Save Value Color3 already tried but doesn’t work

Try making this a Color3Value btw

Don’t remember where i got these functions from, but they work.


function Hex2Color(hex)
    local r,g,b = string.match(hex,"(..)(..)(..)")
    return Color3.fromRGB(tonumber(r,16),tonumber(g,16),tonumber(b,16))
end

function Color2Hex(color)
    local h,e,x = math.floor(color.r*255+.5),math.floor(color.g*255+.5),math.floor(color.b*255+.5)
    return string.format("%02x%02x%02x",h,e,x)
end

To save, put the color value into the Color2Hex function, and to turn it back into a Color3, just use the Hex2Color function.

Example:

local color3 = Color3.fromRGB(255,90,90) --Red, Color3, Cannot save.
local hex = Color2Hex(color3) --Red, hexadecimal, you can save this in datastore, but can't be used in instances

-- To turn it back into a color 3 just do the following:
local newColor = Hex2Color(hex)
1 Like

You can use string:split(",") and create a table with the 3 values that then you can save

1 Like

I tried as you said, but it started giving the error “Unable to cast value to Object”

Make sure you’re passing the value of Color3Value, else it won’t work.

KatanaColor.Value

I’ve never had to deal with Color3)) I can’t even what is what, could you please explain what is what

	if succe then
		Kata.Value = data
		--KatanaC.Value[hex] = data
		KatanaC.Value = data
		
	else
		warn(er)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local succ, err = pcall(function()
		local data = DSS:SetAsync(plr.UserId, plr.MainFolder.Kata.Value, plr.MainFolder.KatanaColor.Value)
		
	end)
		if err then
			warn(err)
end

	game:BindToClose(function()
		for i,plr in pairs(game.Players:GetChildren()) do
			
		end
	end)
end)

Fixed up the errors in your code, this should work.

Removed the ‘KatanaColorSave’ value cause there’s no point in using it.

local DS = game:GetService("DataStoreService")
local DSS = DS:GetDataStore("GameTest1")

function hex2Color(hex)
    local r,g,b = string.match(hex,"(..)(..)(..)")
    return Color3.fromRGB(tonumber(r,16),tonumber(g,16),tonumber(b,16))
end

function color2Hex(color)
    local h,e,x = math.floor(color.r*255+.5),math.floor(color.g*255+.5),math.floor(color.b*255+.5)
    return string.format("%02x%02x%02x",h,e,x)
end

game.Players.PlayerAdded:Connect(function(plr)
    local FKatas = Instance.new("Folder", plr)
    FKatas.Name = "MainFolder" 

    local Kata = Instance.new("IntValue",FKatas)
    Kata.Name = "Kata"
    Kata.Value = 0

    local KatanaC = Instance.new("Color3Value",FKatas)
    KatanaC.Name = "KatanaColor"
    KatanaC.Value = Color3.new(255,255,255)

    local KatanaF = Instance.new("Folder",plr)
    KatanaF.Name = "KatanaFolder"

    local data
    local succe,er = pcall(function()
        data = DSS:GetAsync(plr.UserId)
    end)

    if succe and data then
        Kata.Value = data.Kata
        KatanaC.Value = hex2Color(data.Color)
    else
        warn(er or "No data?")
    end
end)

local function save(plr)
    local succ, err = pcall(function()
        local data = {
            Kata = plr.MainFolder.Kata.Value;
            Color = color2Hex(plr.MainFolder.KatanaColor.Value);
        }
        
        DSS:SetAsync(plr.UserId, data)
    end)
    
    if err then
        warn(err)
    end
end

game.Players.PlayerRemoving:Connect(function(plr)
    save(plr)
end)

game:BindToClose(function()
    for i,plr in pairs(game.Players:GetChildren()) do
        save(plr)
    end
end)

Thank you very much, helped a lot, I read the color preservation code and figured it out right away