How to save Color3- and other values in the same data store?

I followed the data store tutorial by GEILER123456 and thought I could modify it afterwards to save multiple values. I need to save Color3 values in addition to other and that doesn’t seem to happen so easily. I’ve tried several things, but here is how my script looks right now.

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("dataStore1") 
local players = game:GetService("Players")
local runService = game:GetService("RunService")

local dataTable = {
	Cash = 0,
	SkinColor = Color3.new(0,0,0)
}

------------LOAD DATA-----------------
local function setUp(player)
	local userId = player.UserId 
	local playerKey = userId 	

	local leaderStats = Instance.new("Folder")	

	for key,value in pairs(dataTable) do		--Placing all the stats into the "leaderstats" folder
		if typeof(value)== "number" then
			local numberVal = Instance.new("NumberValue")
			numberVal.Name = key
			numberVal.Parent = leaderStats
		else if typeof(value) == "Color3" then
				local colorVal = Instance.new("Color3Value")
				colorVal.Name = key
				colorVal.Parent = leaderStats
			end
		end
	end

	local success,retValue

	repeat
		success,retValue = pcall(dataStore.GetAsync,dataStore,playerKey) 
	until success or not players:FindFirstChild(player.Name)

	if success then
		local CurrentStats = leaderStats:GetDescendants() 

		for orderNum, statName in pairs(CurrentStats) do 	--loading the saved data to my stats (if there is any)
			if typeof (statName.Value) == "Color3" then
				statName.Value = Color3.new(retValue[1],retValue[2],retValue[3]) or Color3.new
			else if typeof (statName.Value) == "number" then
					statName.Value = retValue or 0
				end
			end
		end
		leaderStats.Name = "Stats"
		leaderStats.Parent = player 
		print("Data loaded")
	end
end


----------SAVE DATA--------------
local function save(player, leaving)
	
	if leaving == nil then
		leaving = false
	end
	
	local userId = player.UserId
	local playerKey = userId

	local leaderStats = player:FindFirstChild("Stats")

	if leaderStats then
		
		local currentStats = {}		
		for orderNum, statName in pairs(player.Stats:GetChildren())do  --trying to create a table of the stats
			if typeof (statName.Value) == "Color3" then
				local color = statName.Value
				local colorTable = {color.r,color.g,color.b}
				currentStats[statName.Name] = colorTable
			else
				currentStats[statName.Name]= statName.Value
			end
		end
		repeat
			wait()

			local success, retValue = pcall(function()
				dataStore:SetAsync(playerKey, currentStats)
			end)
			if success then
				print ("Data saved")
			else 
				warn("error while saving data")
			end
		until success
	end
end
-----------------------------------------------------------------

game.Players.PlayerAdded:Connect(setUp)
game.Players.PlayerRemoving:Connect(save)

while true do	--Auto save--
	wait(300)
	for _,player in ipairs(players:GetPlayers()) do
		coroutine.wrap(setUp)(player)
	end
	for _,player in ipairs(players:GetPlayers()) do	
		coroutine.wrap(save)(player) 
	end
end

It doesn’t give errors neither does it save the data. I’m not sure if either Get- or SetAsync is done correctly…

Replace:

if typeof (statName.Value) == "Color3" then
	statName.Value = Color3.new(retValue[1],retValue[2],retValue[3]) or Color3.new
else if typeof (statName.Value) == "number" then
	statName.Value = retValue or 0
end

With:

local data = retValue[statName.Name]
if typeof(statName.Value) == "Color3" and data then
	statName.Value = Color3.new(data[1], data[2], data[3])
elseif typeof(statName.Value) == "number" and data then
	statName.Value = data 
end
1 Like