Help Saving Colors in a DataStore

I know you can’t save colors in a datastore, so I tried making a way to get around it. What I tried isn’t working. I tried converting the color value to a string and it converted, but I can’t get the value.

Script:

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("SaveMoney")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Cash = Instance.new("IntValue")
	Cash.Parent = leaderstats
	Cash.Name = "Cash"

	local RPName = Instance.new("StringValue")
	RPName.Name = "RPName"
	RPName.Parent = player

	local RPBio = Instance.new("StringValue")
	RPBio.Name = "RPBio"
	RPBio.Parent = player
	
	local NameCol = Instance.new("Color3Value")
	NameCol.Name = "NameCol"
	NameCol.Parent = player

	local LoadData
	local success,failure = pcall(function()
		LoadData = PlayerData:GetAsync(player.UserId.."-Data")
	end)
	if success then
		if LoadData ~= nil then
			Cash.Value = LoadData.Cash
			RPName.Value = LoadData.Name or ""
			RPBio.Value = LoadData.Bio or ""
			NameCol.Value = Color3.new(LoadData.NC) or Color3.fromRGB(255, 255, 255)
		else
			Cash.Value = 25000
			RPName.Value = player.Name
			RPBio.Value = ""
			NameCol.Value = Color3.fromRGB(255, 255, 255)
		end
		print("Loaded "..player.Name)
	else
		print("Failed to load "..player.Name)
		warn(failure)
	end
end)

local function SaveData(player)
	local DataToSave = {
		Cash = player.leaderstats.Cash.Value;
		Name = player.RPName.Value;
		Bio = player.RPBio.Value;
		NC = tostring(player.NameCol.Value)
	}
	local success, failure = pcall(function()
		PlayerData:SetAsync(player.UserId.."-Data", DataToSave)
	end)
	if not success then
		print("Failed to save "..player.Name)
		warn(failure)
	end
end

local gameClosing = false

game.Players.PlayerRemoving:Connect(function(player)
	wait()
	if not gameClosing then
		SaveData(player)
	end
end)

game:BindToClose(function()
	gameClosing = true
	for _,player in pairs(game.Players:GetChildren()) do
		SaveData(player)
	end
end)

It’s the NameCol value that gets messed up.

1 Like

You can do this by using a table to store the color values, then generate the color on load.

function SaveData(...)
...
	local NC = {}
NC.R = player.NameCol.Value.R
NC.G = player.NameCol.Value.G
NC.B = player.NameCol.Value.B
	}
...
function LoadData(...)
...
NameCol.Value = Color3.fromRGB(LoadData.NC.R,LoadData.NC.G,LoadData.NC.B) or Color3.fromRGB(255, 255, 255)
2 Likes

Color3.new takes 3 numbers, not a single string. You’ll have to split the string as shown here:

as @Tengarapatu said, you will have to store the colors separately in a table, then on load, construct a new Color3 with those 3 parameters

local color_table = {
 red =,
 green = ,
 blue = ,
}

-- on load
local new_color = Color3.fromRGB(color_table[red],color_table[green],color_table[blue])

I can’t get it to work.

Current Code:

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("SaveMoney")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Cash = Instance.new("IntValue")
	Cash.Parent = leaderstats
	Cash.Name = "Cash"

	local RPName = Instance.new("StringValue")
	RPName.Name = "RPName"
	RPName.Parent = player

	local RPBio = Instance.new("StringValue")
	RPBio.Name = "RPBio"
	RPBio.Parent = player

	local NameCol = Instance.new("Color3Value")
	NameCol.Name = "NameCol"
	NameCol.Parent = player

	local LoadData
	local success,failure = pcall(function()
		LoadData = PlayerData:GetAsync(player.UserId.."-Data")
	end)
	if success then
		if LoadData ~= nil then
			Cash.Value = LoadData.Cash
			RPName.Value = LoadData.Name or ""
			RPBio.Value = LoadData.Bio or ""
			NameCol.Value = Color3.fromRGB(LoadData.NC.R,LoadData.NC.G,LoadData.NC.B) or Color3.fromRGB(255, 255, 255)
		else
			Cash.Value = 25000
			RPName.Value = player.Name
			RPBio.Value = ""
			NameCol.Value = Color3.fromRGB(255, 255, 255)
		end
		print("Loaded "..player.Name)
	else
		print("Failed to load "..player.Name)
		warn(failure)
	end
end)

local function SaveData(player)
	local NC = {}
	NC.R = player.NameCol.Value.R
	NC.G = player.NameCol.Value.G
	NC.B = player.NameCol.Value.B
	print(NC)
	local DataToSave = {
		Cash = player.leaderstats.Cash.Value;
		Name = player.RPName.Value;
		Bio = player.RPBio.Value;
		NC
	}
	local success, failure = pcall(function()
		PlayerData:SetAsync(player.UserId.."-Data", DataToSave)
	end)
	if not success then
		print("Failed to save "..player.Name)
		warn(failure)
	end
end

local gameClosing = false

game.Players.PlayerRemoving:Connect(function(player)
	wait()
	if not gameClosing then
		SaveData(player)
	end
end)

game:BindToClose(function()
	gameClosing = true
	for _,player in pairs(game.Players:GetChildren()) do
		SaveData(player)
	end
end)

Use commas, not semicolons. Tables are split with commas.

It was working just fine before.

I believe, it would fail to load the first time, as it loads the old data. Try changing the name of the Datastore.

I changed the name, but it didn’t save my data.

Found a post regarding this. Check it out

It keeps saying "Attempt to index nil with ‘R’ "

I saw this post too, I couldn’t figure out what to do.

NC.R = player.NameCol.Value.R
I think this gives the R value from 0-1 instead of 0-255, so don’t use fromRGB when loading it
I’m not sure about the error though

NameCol.Value = Color3.new(LoadData[1].R,LoadData[1].G,LoadData[1].B) or Color3.fromRGB(255, 255, 255)
I don’t know why yet, but this works

Edit: You forgot to do this

local DataToSave = {
		Cash = player.leaderstats.Cash.Value;
		Name = player.RPName.Value;
		Bio = player.RPBio.Value;
		NC = NC
	}