How do I save a ColorValue in a DataStore and Get the data from a DataStore?

I wanted to make that a player picks a color and if the Player leaves the data saves. Wich I think it works but the problem is how do I make it to read the data and put it into the ColorValue of the player?

This is how it looks. (All the values)
image

I Tried with that:

Snippet Code (in the PlayerAdded function [Line 59 - 72]):

	local dataofuser = ds:GetAsync(tostring(plr.UserId).."_UserLocker")
	if dataofuser ~= nil then -- anything but nil
		print("Found data for " .. plr.Name)
	--	RadioOwned.Value = dataofuser[1]
		--------------------------------
		RadioEquiped.Value = dataofuser[4]
		ColorChoosen.Value = Color3.fromRGB(dataofuser[1].R, dataofuser[2].G, dataofuser[3].B)
		--BlueCarSkin.Value = dataofuser[1]
		--BlueCarSkinO.Value = dataofuser[1]
		print(ColorChoosen.Value.."")
		print(RadioEquiped.Value.."")
	else
		print("Replacing no data with new data.")
	end

Output Error:
ServerScriptService.DataSaving:65: attempt to index boolean with 'R' - Server - DataSaving:65

This is the DataSaving Script in the ServerScriptService this is the whole script.

--// SERVICES
local DataStoreService = game:GetService("DataStoreService")

--// VARS
local ds = DataStoreService:GetDataStore("PlayerLocker")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local RadiopassID = 663864800

local function checkGamePass(player, passID)
	local hasPass = false
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
	end)
	if not success then
		warn("Error while checking if player has game pass: " .. tostring(message))
		return false
	end
	return hasPass
end

local function saveData(plr)
	local datasave = {}
	local folderitemsowned = plr:FindFirstChild("ItemsOwned")
	local folderitemequiped = plr:FindFirstChild("ItemsEquiped")
	--	table.insert(datasave, checkGamePass(plr, RadiopassID))
	--------------------------
	table.insert(datasave, 1 , folderitemequiped:FindFirstChild("RadioEquiped").Value)
--  table.insert(datasave, 2 , plr:FindFirstChild("ColorChoosen").Value)
--	table.insert(datasave, 2 , folderitemequiped:FindFirstChild("BlueCarSkinEquiped").Value)
--	table.insert(datasave, 3 , folderitemsowned:FindFirstChild("BlueCarSkinOwned").Value)
	
	ds:SetAsync(tostring(plr.UserId).."_UserLocker", datasave)
end

game.Players.PlayerAdded:Connect(function(plr)
	print("executed")
	
	local ColorChoosen = Instance.new("Color3Value", plr)
	ColorChoosen.Name = "ColorChoosen"

	local FolderOwned = Instance.new("Folder",plr)
	FolderOwned.Name = "ItemsOwned"

	local RadioOwned = Instance.new("BoolValue",FolderOwned)
	RadioOwned.Name = "RadioOwned"
	local BlueCarSkinO = Instance.new("BoolValue", FolderOwned)
	BlueCarSkinO.Name = "BlueCarSkinOwned"
	----------------------------------------------------

	local FolderEquiped = Instance.new("Folder", plr)
	FolderEquiped.Name = "ItemsEquiped"

	local RadioEquiped = Instance.new("BoolValue",FolderEquiped)
	RadioEquiped.Name = "RadioEquiped"
	local BlueCarSkin = Instance.new("BoolValue", FolderEquiped)
	BlueCarSkin.Name = "BlueCarSkinEquiped"

	local dataofuser = ds:GetAsync(tostring(plr.UserId).."_UserLocker")
	if dataofuser ~= nil then -- anything but nil
		print("Found data for " .. plr.Name)
	--	RadioOwned.Value = dataofuser[1]
		--------------------------------
		RadioEquiped.Value = dataofuser[4]
		ColorChoosen.Value = Color3.fromRGB(dataofuser[1].R, dataofuser[2].G, dataofuser[3].B)
		--BlueCarSkin.Value = dataofuser[1]
		--BlueCarSkinO.Value = dataofuser[1]
		print(ColorChoosen.Value.."")
		print(RadioEquiped.Value.."")
	else
		print("Replacing no data with new data.")
	end
	

	if checkGamePass(plr, RadiopassID) then
		-- Player owns the game pass
		print("Player owns the game pass")
		RadioOwned.Value = true
	else
		print("Player does not own the game pass")
		RadioEquiped.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	print(plr.Name .. " is leaving!!")
	local datasave = {plr:FindFirstChild("ColorChoosen").Value.R, plr:FindFirstChild("ColorChoosen").Value.G, plr:FindFirstChild("ColorChoosen").Value.B, }
	local folderitemsowned = plr:FindFirstChild("ItemsOwned")
	local folderitemequiped = plr:FindFirstChild("ItemsEquiped")
--	table.insert(datasave, checkGamePass(plr, RadiopassID))
	--------------------------
	table.insert(datasave, 4 , folderitemequiped:FindFirstChild("RadioEquiped").Value)

	local success,response = pcall(function()
		ds:SetAsync(tostring(plr.UserId).."_UserLocker", datasave)
	end)

	if success then
		print("succesfully saved data of " .. plr.Name)
	else
		warn(response)
	end
end)

--game:BindToClose(function()
	--for i, p in pairs(game.Players:GetPlayers()) do
		--saveData(p)
	--end
--end)
6 Likes

Google the issue, search for an answer.

2 Likes

Yea… Tried that too. When I would have the awnser on google I would not be on the forums asking for help lol

3 Likes

But thanks! Let me see and try that out

1 Like

I tried it with getting the converted Hex value like this:

local ColorHexChoosen = Color3.fromHex(dataofuser[2])
ColorChoosen.Value = ColorHexChoosen

And save it like this:

local HexColor3Value = plr:FindFirstChild("ColorChoosen").Value:ToHex()
table.insert(datasave, 2 , HexColor3Value)

Hope it not wrong.

Full code.

game.Players.PlayerAdded:Connect(function(plr)
	print("executed")
	
	local ColorChoosen = Instance.new("Color3Value", plr)
	ColorChoosen.Name = "ColorChoosen"

	local FolderOwned = Instance.new("Folder",plr)
	FolderOwned.Name = "ItemsOwned"

	local RadioOwned = Instance.new("BoolValue",FolderOwned)
	RadioOwned.Name = "RadioOwned"
	local BlueCarSkinO = Instance.new("BoolValue", FolderOwned)
	BlueCarSkinO.Name = "BlueCarSkinOwned"
	----------------------------------------------------

	local FolderEquiped = Instance.new("Folder", plr)
	FolderEquiped.Name = "ItemsEquiped"

	local RadioEquiped = Instance.new("BoolValue",FolderEquiped)
	RadioEquiped.Name = "RadioEquiped"
	local BlueCarSkin = Instance.new("BoolValue", FolderEquiped)
	BlueCarSkin.Name = "BlueCarSkinEquiped"

	local dataofuser = ds:GetAsync(tostring(plr.UserId).."_UserLocker")
	if dataofuser ~= nil then -- anything but nil
		print("Found data for " .. plr.Name)
	--	RadioOwned.Value = dataofuser[1]
		--------------------------------
		local ColorHexChoosen = Color3.fromHex(dataofuser[2])
		--------------------------------
		RadioEquiped.Value = dataofuser[1]
		ColorChoosen.Value = ColorHexChoosen
		--BlueCarSkin.Value = dataofuser[1]
		--BlueCarSkinO.Value = dataofuser[1]
		print(ColorChoosen.Value.."")
		print(RadioEquiped.Value.."")
	else
		print("Replacing no data with new data.")
	end
	

	if checkGamePass(plr, RadiopassID) then
		-- Player owns the game pass
		print("Player owns the game pass")
		RadioOwned.Value = true
	else
		print("Player does not own the game pass")
		RadioEquiped.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	print(plr.Name .. " is leaving!!")
	local datasave = {plr:FindFirstChild("ColorChoosen").Value.R, plr:FindFirstChild("ColorChoosen").Value.G, plr:FindFirstChild("ColorChoosen").Value.B, }
	local folderitemsowned = plr:FindFirstChild("ItemsOwned")
	local folderitemequiped = plr:FindFirstChild("ItemsEquiped")
	local HexColor3Value = plr:FindFirstChild("ColorChoosen").Value:ToHex()
--	table.insert(datasave, checkGamePass(plr, RadiopassID))
	--------------------------
	table.insert(datasave, 1 , folderitemequiped:FindFirstChild("RadioEquiped").Value)
	table.insert(datasave, 2 , HexColor3Value)

	local success,response = pcall(function()
		ds:SetAsync(tostring(plr.UserId).."_UserLocker", datasave)
	end)

	if success then
		print("succesfully saved data of " .. plr.Name)
	else
		warn(response)
	end
end)
2 Likes

why do you need these 3 values in the array if you’re saving the hex value anyway? I would just save the hex value by itself

I would also add that it’s probably best for future readability that you save this table in dictionary format, just for readability purposes. I used to do exactly this when I was new so I understand the impulse.

For example, consider this table (array format):

local truck = {Color3.new(1, 0, 1), 4, {}}

Looking at that table alone, you wouldn’t be able to figure out what those values you mean. The table represents a truck, there’s a color value, maybe that’s the color of the truck? But who knows what the other entries mean.

However, if I used a dictionary to label it, rather than just trying to remember the meaning of each index number and its respective value, then it could be readable without subsequent variable names or comments or anything:

local truck = {PaintColor = Color3.new(1, 0, 1), Wheels = 4, UpgradeList = {}}

Now those table entries are self-explanatory. This format of table in Lua is called a dictionary, where the indices are explicitly defined. Not sure if that’s the exact technical definition but that’s the common understanding.

3 Likes

I see! In the meanwhile for like 1 - 2 hours I switched to BrickColorValues

And made this code but seems like its not storing it either!
With no error nothing.

--// SERVICES
local DataStoreService = game:GetService("DataStoreService")

--// VARS
local ds = DataStoreService:GetDataStore("PlayerLocker")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local RadiopassID = 663864800

local function checkGamePass(player, passID)
	local hasPass = false
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
	end)
	if not success then
		warn("Error while checking if player has game pass: " .. tostring(message))
		return false
	end
	return hasPass
end

local function saveData(plr)
	local datasave = {}
	local folderitemsowned = plr:FindFirstChild("ItemsOwned")
	local folderitemequiped = plr:FindFirstChild("ItemsEquiped")
	--	table.insert(datasave, checkGamePass(plr, RadiopassID))
	--------------------------
	table.insert(datasave, 1 , folderitemequiped:FindFirstChild("RadioEquiped").Value)
--  table.insert(datasave, 2 , plr:FindFirstChild("ColorChoosen").Value)
--	table.insert(datasave, 2 , folderitemequiped:FindFirstChild("BlueCarSkinEquiped").Value)
--	table.insert(datasave, 3 , folderitemsowned:FindFirstChild("BlueCarSkinOwned").Value)
	
	ds:SetAsync(tostring(plr.UserId).."_UserLocker", datasave)
end

game.Players.PlayerAdded:Connect(function(plr)
	print("executed")

	local FolderOwned = Instance.new("Folder",plr)
	FolderOwned.Name = "ItemsOwned"

	local RadioOwned = Instance.new("BoolValue",FolderOwned)
	RadioOwned.Name = "RadioOwned"
	local BlueCarSkinO = Instance.new("BoolValue", FolderOwned)
	BlueCarSkinO.Name = "BlueCarSkinOwned"
	----------------------------------------------------

	local FolderEquiped = Instance.new("Folder", plr)
	FolderEquiped.Name = "ItemsEquiped"

	local RadioEquiped = Instance.new("BoolValue",FolderEquiped)
	RadioEquiped.Name = "RadioEquiped"
	local BlueCarSkin = Instance.new("BoolValue", FolderEquiped)
	BlueCarSkin.Name = "BlueCarSkinEquiped"
	
	local ColorChoosen = Instance.new("BrickColorValue")
	ColorChoosen.Name = "ColorChoosen"
	ColorChoosen.Parent = plr

	local dataofuser = ds:GetAsync(tostring(plr.UserId).."_UserLocker")
	if dataofuser ~= nil then -- anything but nil
		print("Found data for " .. plr.Name)
	--	RadioOwned.Value = dataofuser[1]
		--------------------------------
		RadioEquiped.Value = dataofuser[1]
		ColorChoosen.Value = BrickColor.new(tostring(dataofuser[2]))
		--BlueCarSkin.Value = dataofuser[1]
		--BlueCarSkinO.Value = dataofuser[1]
	else
		print("Replacing no data with new data.")
	end
	

	if checkGamePass(plr, RadiopassID) then
		-- Player owns the game pass
		print("Player owns the game pass")
		RadioOwned.Value = true
	else
		print("Player does not own the game pass")
		RadioEquiped.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	print(plr.Name .. " is leaving!!")
	local datasave = {}
	local folderitemsowned = plr:FindFirstChild("ItemsOwned")
	local folderitemequiped = plr:FindFirstChild("ItemsEquiped")
	local ColorValue = plr:FindFirstChild("ColorChoosen").Value
	local ColorValueToString = tostring(ColorValue)
--	table.insert(datasave, checkGamePass(plr, RadiopassID))
	--------------------------
	table.insert(datasave, 1 , folderitemequiped:FindFirstChild("RadioEquiped").Value)
	table.insert(datasave, 2 , ColorValueToString)

	local success,response = pcall(function()
		ds:SetAsync(tostring(plr.UserId).."_UserLocker", datasave)
	end)

	if success then
		print("succesfully saved data of " .. plr.Name)
	else
		warn(response)
	end
end)

--game:BindToClose(function()
	--for i, p in pairs(game.Players:GetPlayers()) do
		--saveData(p)
	--end
--end)
2 Likes

I’m not sure if this helps you, but the way I save colors is by putting them in a dictionary and then converting them to JSON. For example:

datasave["Colors"] = {["R"] = YourColor.R, ["G"] = YourColor.G, ["B"] = YourColor.B}

Then when you are saving the data, you can do something along the lines of

local JSONData = HttpService:JSONEncode(datasave)
YourDS:SetAsync(player.UserId.."_UserLocker", JSONData)

By the way, you should put your GetAsync and SetAsync inside of a pcall.

3 Likes

How many car colors do you have? Do you have to save the format or just the color they selected?
Could be as simple as 1,2,3 …

Read data ... CarColor

if CarColor== 1 then
    color = Color3.new(1, 0, 0)  -- Red
elseif CarColor== 2 then
    color = Color3.new(0, 1, 0)  -- Green
elseif CarColor== 3 then
    color = Color3.new(0, 0, 1)  -- Blue
else
    color = Color3.new(1, 1, 1)  -- Default
end
...
local DataStoreService = game:GetService("DataStoreService")
local ColorDataStore = DataStoreService:GetDataStore("ColorDataStore")
local key = "userColorKey"

local colorToSave = Color3.fromRGB(123, 45, 89)
ColorDataStore:SetAsync(key, {R = colorToSave.R, G = colorToSave.G, B = colorToSave.B})

local savedColorData = ColorDataStore:GetAsync(key)

if savedColorData then
    local savedColor = Color3.new(savedColorData.R, savedColorData.G, savedColorData.B)
    ColorChoosen.Value = savedColor
end

local DataStoreService = game:GetService("DataStoreService")
local ColorDataStore = DataStoreService:GetDataStore("ColorDataStore")
local key = "userColorKey"

local colorToSave = Color3.fromRGB(dataofuser[1], dataofuser[2], dataofuser[3])
ColorDataStore:SetAsync(key, {R = colorToSave.R, G = colorToSave.G, B = colorToSave.B})

local savedColorData = ColorDataStore:GetAsync(key)

if savedColorData then
    local savedColor = Color3.new(savedColorData.R, savedColorData.G, savedColorData.B)
    ColorChoosen.Value = savedColor
end

4 Likes

Yes I will try this out thank you so much!

2 Likes

Good idea! But for no I will stick to the method that @2112Jay was showing me

--// SERVICES
local DataStoreService = game:GetService("DataStoreService")

--// VARS
local ds = DataStoreService:GetDataStore("PlayerLockerCapybara")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local RadiopassID = 663864800

local function checkGamePass(player, passID)
	local hasPass = false
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
	end)
	if not success then
		warn("Error while checking if player has game pass: " .. tostring(message))
		return false
	end
	return hasPass
end

local function saveData(plr)
	local datasave = {}
	local folderitemsowned = plr:FindFirstChild("ItemsOwned")
	local folderitemequiped = plr:FindFirstChild("ItemsEquiped")
	--	table.insert(datasave, checkGamePass(plr, RadiopassID))
	--------------------------
	table.insert(datasave, 1 , folderitemequiped:FindFirstChild("RadioEquiped").Value)
--  table.insert(datasave, 2 , plr:FindFirstChild("ColorChoosen").Value)
--	table.insert(datasave, 2 , folderitemequiped:FindFirstChild("BlueCarSkinEquiped").Value)
--	table.insert(datasave, 3 , folderitemsowned:FindFirstChild("BlueCarSkinOwned").Value)
	
	ds:SetAsync(tostring(plr.UserId).."_UserLocker", datasave)
end

game.Players.PlayerAdded:Connect(function(plr)
	print("executed")

	local FolderOwned = Instance.new("Folder",plr)
	FolderOwned.Name = "ItemsOwned"

	local RadioOwned = Instance.new("BoolValue",FolderOwned)
	RadioOwned.Name = "RadioOwned"
	local BlueCarSkinO = Instance.new("BoolValue", FolderOwned)
	BlueCarSkinO.Name = "BlueCarSkinOwned"
	----------------------------------------------------

	local FolderEquiped = Instance.new("Folder", plr)
	FolderEquiped.Name = "ItemsEquiped"

	local RadioEquiped = Instance.new("BoolValue",FolderEquiped)
	RadioEquiped.Name = "RadioEquiped"
	local BlueCarSkin = Instance.new("BoolValue", FolderEquiped)
	BlueCarSkin.Name = "BlueCarSkinEquiped"
	
	local ColorChoosen = Instance.new("NumberValue", FolderEquiped)
	ColorChoosen.Name = "ColorChoosen"

	local dataofuser = ds:GetAsync(tostring(plr.UserId).."_UserLocker")
	if dataofuser ~= nil then -- anything but nil
		print("Found data for " .. plr.Name)
	--	RadioOwned.Value = dataofuser[1]
		--------------------------------
		RadioEquiped.Value = dataofuser[1]
		ColorChoosen.Value = dataofuser[2]
		--BlueCarSkin.Value = dataofuser[1]
		--BlueCarSkinO.Value = dataofuser[1]
		print("ColorChoose.Value = "..dataofuser[2])
	else
		print("Replacing no data with new data.")
	end
	

	if checkGamePass(plr, RadiopassID) then
		-- Player owns the game pass
		print("Player owns the game pass")
		RadioOwned.Value = true
	else
		print("Player does not own the game pass")
		RadioEquiped.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	print(plr.Name .. " is leaving!!")
	local folderitemsowned = plr:FindFirstChild("ItemsOwned")
	local folderitemequiped = plr:FindFirstChild("ItemsEquiped")
	local datasave = {}
--	table.insert(datasave, checkGamePass(plr, RadiopassID))
	--------------------------
	table.insert(datasave, 1 , folderitemequiped:FindFirstChild("RadioEquiped").Value)
	table.insert(datasave, 2 , folderitemequiped:FindFirstChild("ColorChoosen").Value)

	local success,response = pcall(function()
		ds:SetAsync(tostring(plr.UserId).."_UserLocker", datasave)
	end)

	if success then
		print("succesfully saved data of " .. plr.Name)
	else
		warn(response)
	end
end)

--game:BindToClose(function()
	--for i, p in pairs(game.Players:GetPlayers()) do
		--saveData(p)
	--end
--end)

Idk why it does not save or get the value correctly for both of them.

2 Likes

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