Problem with saving color RGB values to table

Hello DevFourm, I am making a game where you can customize a part and spawn it into your PlayArea! I am working on a saving system to same the parts the have spawned. To do this, I created a table to store all of your parts Colors, Positions, and Material. However, when loading the table and converting it form a JSON table to a Lua table, the color does not save and the part loaded is color code (0,0,0).

My Code:

-- Services
local dataStoreServce = game:GetService("DataStoreService")
local playerData = dataStoreServce:GetDataStore("PlayerParts")
local players = game:GetService("Players")
local http = game:GetService("HttpService")

-- Veriables
local prefix = "Player Part Save: "
local parts = game.Workspace.PlayArea

local function saveData(player)
	local dataKey = prefix.. tostring(player.UserId)
	local data = {}
	
	for i, v in ipairs(parts:GetChildren()) do
		table.insert(data, {
			tonumber(v.Color.R),
			tonumber(v.Color.G),
			tonumber(v.Color.B),
			v.Position.X,
			v.Position.Y,
			v.Position.Z,
			v.Material
			
		})
	end
	
	local complete, errorMsg
	
	repeat
		complete, errorMsg = pcall(function()
			playerData:UpdateAsync(dataKey, function()
				local data_encode = http:JSONEncode(data)
				return data_encode
			end)
		end)
		
		task.wait(0.5)
	until complete
	
	if complete then
		print("Data Save Complete!")
		
	else
		warn("Data Save Error: ".. errorMsg)
	end
end

local function loadData(player)
	local key = prefix ..  tostring(player.UserId)
	local data
	
	local complete, errorMsg
	
	repeat
		complete, errorMsg = pcall(function()
			data = http:JSONDecode(playerData:GetAsync(key))
			print(data)
		end)
	until complete or not players:FindFirstChild(player.Name)
	
	if not data then
		return
	end
	
	if complete then
		for i, v in ipairs(data) do
			local part = Instance.new("Part")
			part.Color = Color3.fromRGB(v[1], v[2], v[3])
			part.Position = Vector3.new(v[4], v[5], v[6])
			--part.Material = v[7]
			part.Parent = game.Workspace.PlayArea
		end
		
	else
		
		warn("Data Error:" .. errorMsg)
	end
end


players.PlayerAdded:Connect(loadData)
players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
	for i, player in ipairs(players:GetPlayers()) do
		saveData(player)
	end
end)

Part Before Save:
image

Part After Save:
image

This is the table loaded from the DataStore:
image
(1, 2 and 3 are color RGB values)
(4, 5 and 6 are positon XYZ values)

Thank you :slight_smile:

The values are probably in the range of 0 to 1, so load the part using Color3.new instead of Color3.fromRGB

1 Like

This worked! Thank you for your help! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.