Color saving datastore

So what im trying to achieve is saving Color3Values and then loading them again. My current code looks like this

Loading:

game.Players.PlayerAdded:Connect(function(player)
	local vehiclesFolder = Instance.new("Model",workspace)
	vehiclesFolder.Name = player.Name.." Vehicles"
	local folder = Instance.new("Folder",player)
	folder.Name="leaderstats"
	local codes = Instance.new("Folder",player)
	codes.Name="Codes"
	local other = Instance.new("Folder",player)
	other.Name="Other"
	local awardedCars = Instance.new("Folder",workspace)
	awardedCars.Name = "awardedCars_"..player.Name
	local money = Instance.new("IntValue",folder)
	money.Name="Money"
	local vf=Instance.new("Folder",player)
	vf.Name="OwnedVehicles"
	for i,companies in pairs(game.ReplicatedStorage.Vehicles:GetChildren()) do
		for i,car in pairs(companies:GetChildren()) do
		local c=Instance.new("IntValue",vf)
		c.Name=car.Name
		local carColor = Instance.new("Color3Value")
		carColor.Parent = player.OwnedVehicles[car.Name]
		carColor.Name = "BodyColor"
		end
	end
	for i,coder in pairs(code) do
		local c=Instance.new("IntValue",codes)
		c.Name=code[i]:lower()
	end
	for i,o in pairs(others) do
		local c=Instance.new(others[i].Type,other)
		c.Name=others[i].Name
		c.Value=others[i].Value
	end
	
	local key = "fullboostkey_" .. tostring(player.UserId)
	
	print("Player "..player.Name.." joined with the key "..key)
	
	local data
	local carData
	local codeData
	local customizationData
	
	local suc,fai = pcall(function()
	data = driftDataStore:GetAsync(key)
	carData = vehicleData:GetAsync(key)
	codeData = codeDatastore:GetAsync(key)
	customizationData = customization:GetAsync(key)
	end)
	
	if suc then
		if data.Money then
		player.leaderstats.Money.Value = data.Money
		else
		print("User "..player.Name.." has no stats!")
		player.leaderstats.Money.Value = 0
		end
		for i,companies in pairs(game.ReplicatedStorage.Vehicles:GetChildren()) do
			for i,car in pairs(companies:GetChildren()) do
			player.OwnedVehicles[car.Name].Value=carData[car.Name]
			if customizationData and customizationData[car.Name] then
			player.OwnedVehicles[car.Name].BodyColor.Value=customizationData[car.Name]
				else
			player.OwnedVehicles[car.Name].BodyColor.Value=Color3.fromRGB(248, 248, 248)
			end
			end
		end
		for i,c in pairs(codes:GetChildren()) do
			c.Value=codeData[code[i]:lower()]
		end
		print("Sucessfully loaded data for "..player.Name)
	else
		print("Failed to load data for "..player.Name)
		warn(fai)
	end
	
	if data then
	else
	
	end
end)

Saving:

game.Players.PlayerRemoving:Connect(function(player)
	
	local key = "fullboostkey_" .. tostring(player.UserId)
	
	print("Player leaving with key: "..key)
	
	local m={
	Money = player.leaderstats.Money.Value;
	}
	local n={}
	local x={}
	local c={}
	
	for i,car in pairs(player.OwnedVehicles:GetChildren()) do
		n[car.Name]=player.OwnedVehicles[car.Name].Value
		c[car.Name]=player.OwnedVehicles[car.Name].BodyColor.Value
	end
	for i,c in pairs(player.Codes:GetChildren()) do
		x[code[i]:lower()]=player.Codes[code[i]:lower()].Value
	end

	local success,failure = pcall(function()
		driftDataStore:SetAsync(key,m)
		vehicleData:SetAsync(key,n)
		codeDatastore:SetAsync(key,x)
		customization:SetAsync(key,c)
	end)
	
	if success then
		print("Successfully saved data!")
	else
		print("There was an error while saving data!")
		warn(failure)
	end
end)

I’ve tried alot but it just reverts to default white which means data must have not been saved?

1 Like

I don’t think you can save Color3 values to data stores, so you’d have to store the values in another format such as a table. For example:

local color = player.OwnedVehicles[car.Name].BodyColor.Value
c[car.Name] = {color.R, color.G, color.B}

Then later when you are loading the data:

local colorTable = customizationData[car.Name]
player.OwnedVehicles[car.Name].BodyColor.Value=Color3.new(colorTable[1], colorTable[2], colorTable[3])
2 Likes

Ah, i was thinking of doing that, but yeah i’ll try it out

Ok, so another issue, it’s turning black when they have no colour data, is there anyway to make it so it turns white when they have no data, i tried doing a check but it didn’t seem to work

What does the check look like now, after using the new change?

Here is the new check

if customizationData and customizationData[car.Name] then
local colorTable = customizationData[car.Name]
player.OwnedVehicles[car.Name].BodyColor.Value=Color3.new(colorTable[1], colorTable[2], colorTable[3])
else
player.OwnedVehicles[car.Name].BodyColor.Value=Color3.fromRGB(248, 248, 248)
end
end

Maybe check if one of the values in the color table is nil to see if it has any data. An empty table still evaluates to true.

local colorTable = customizationData[car.Name]
if customizationData and colorTable and colorTable[1] then
player.OwnedVehicles[car.Name].BodyColor.Value=Color3.new(colorTable[1], colorTable[2], colorTable[3])
else
player.OwnedVehicles[car.Name].BodyColor.Value=Color3.fromRGB(248, 248, 248)
end
end

Still turns out black for some reason, pretty sure when you insert a color3value it standards at 0,0,0 which is black, maybe if i made it start at white it would work?

Ok so i attempted to clear my data and what happened was it gave me this error:
ServerScriptService.LeaderstatsV2:103: attempt to index nil with ‘AudiA5’

The values have also gone to 63240, 63240, 63240 which is weird

One way you can do it is using string formatting.

--Loading
local r, g, b = string.match(v, "^(.-), (.-), (.+)")
playerData.Colors[i].Value = Color3.new(tonumber(r), tonumber(g), tonumber(b))
--Saving
saveData.Colors[v.Name] = tostring(v.Value) --will return the color in format of 0, 0, 0
1 Like