Help with saving color

hello, does anyone know why it saves the CFrame, but not the Color?

local BuildingManager = {}
local Services = require(game:GetService("ReplicatedStorage").modules.services)
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerBuildingData")

function BuildingManager.Build(Player: Player, CF: CFrame, Color: Color3)
	local PlayerPlot
	for _, v in pairs(Services.Workspace.Zones:GetChildren()) do
		if v.Username.Value == Player.Name then
			PlayerPlot = v
			break
		end
	end

	if not PlayerPlot then return end

	local Block = Services.ReplicatedStorage.templates.Block:Clone()
	Block.Parent = PlayerPlot.Blocks
	Block.CFrame = CF
	Block.Color = Color or Color3.new(1, 1, 1)

	-- Save CFrame and Color data
	local success, err = pcall(function()
		local existingData = PlayerDataStore:GetAsync(tostring(Player.UserId)) or {}
		table.insert(existingData, {
			CFrame = {CF:GetComponents()},
			Color = {Color.R, Color.G, Color.B}
		})
		PlayerDataStore:SetAsync(tostring(Player.UserId), existingData)
	end)

	if not success then
		warn("Failed to save building data: " .. err)
	else
		print("Block saved successfully.")
	end
end

function BuildingManager.Load(Player: Player)
	local PlayerPlot
	for _, v in pairs(Services.Workspace.Zones:GetChildren()) do
		if v.Username.Value == Player.Name then
			PlayerPlot = v
			break
		end
	end

	if not PlayerPlot then return end

	local success, data = pcall(function()
		return PlayerDataStore:GetAsync(tostring(Player.UserId))
	end)

	if success and data then
		for _, blockData in ipairs(data) do
			if blockData.CFrame and blockData.Color then
				local cfData = blockData.CFrame
				local colorData = blockData.Color

				local Block = Services.ReplicatedStorage.templates.Block:Clone()
				Block.Parent = PlayerPlot.Blocks
				Block.CFrame = CFrame.new(unpack(cfData))
				Block.Color = Color3.new(unpack(colorData))
			end
		end
		print("Blocks loaded successfully.")
	elseif not success then
		warn("Failed to load building data: " .. data)
	end
end

local function onBlockClick(Player, Block)
	local colors = {
		Color3.fromRGB(255, 0, 0),  -- Red
		Color3.fromRGB(0, 255, 0),  -- Green
		Color3.fromRGB(0, 0, 255),  -- Blue
		Color3.fromRGB(255, 255, 0), -- Yellow
		Color3.fromRGB(0, 255, 255), -- Cyan
		Color3.fromRGB(255, 0, 255),  -- Magenta
	}
	local newColor = colors[math.random(#colors)]

	Block.Color = newColor

	local success, err = pcall(function()
		local existingData = PlayerDataStore:GetAsync(tostring(Player.UserId)) or {}
		local blockFound = false
		for _, blockData in ipairs(existingData) do
			if blockData.CFrame then
				local blockPosition = Vector3.new(unpack(blockData.CFrame))
				if (blockPosition - Block.Position).Magnitude < 0.1 then
					blockData.Color = {newColor.R, newColor.G, newColor.B}
					blockFound = true
					break
				end
			end
		end
		if not blockFound then
			table.insert(existingData, {
				CFrame = {Block.CFrame:GetComponents()},
				Color = {newColor.R, newColor.G, newColor.B}
			})
		end
		PlayerDataStore:SetAsync(tostring(Player.UserId), existingData)
	end)

	if not success then
		warn("Failed to save updated color data: " .. err)
	else
		print("Block color updated and saved.")
	end
end

Services.ReplicatedStorage.events.BlockClicked.OnServerEvent:Connect(onBlockClick)

game.Players.PlayerAdded:Connect(function(player)
	task.wait(1)
	BuildingManager.Load(player)
end)

return BuildingManager
2 Likes

instead of Block.Color can you use Block.Color3?

1 Like

yeah, but it still does not load or save the color

instead of

try

Block.Color = Color3.fromRGB(unpack(colorData))

and also try printing

print(unpack(colorData))

to see what it returns

1 Like

this is what it prints, still doesnt save:

just save Color = Color:toHEX()

1 Like