Cant save datastore 2 colors

when i tried to save the color but it not saves and gives error

  1. i tried looking devforum but not worked

script:

local rs = game:GetService("ReplicatedStorage")
local data = require(game:GetService("ServerScriptService").DataStore2)
local test = require(script.test)

data.Combine("newonelol", "blood", "text")

local function convertStringToColor3(str)
	return Color3.new(table.unpack(str:split(","))); -- the values in the string are seperated by a ",", remember?
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local valueStore = data("blood",plr)
	local textStore = data("text",plr)
	
	local plrgui = plr.PlayerGui
	local settingsF = plrgui:WaitForChild("MainUI").SettingsFrame
	local cloneExample = script.Example:Clone()
	cloneExample.Parent = rs:WaitForChild("plrSettings")
	local blood = cloneExample:WaitForChild("BloodEnabled")
	local text = settingsF.BloodSet.Frame
	local firstString = "85, 170, 127"
	local SecS = "113, 1, 1"
	local first = convertStringToColor3(firstString);
	local sec = convertStringToColor3(SecS)
	cloneExample.Name = plr.Name
	valueStore:OnUpdate(function(new)
		blood.Value = new
	end)
	textStore:OnUpdate(function(new)
		text.BackgroundColor3 = new
	end)
	settingsF.BloodSetButton.MouseButton1Click:Connect(function()
		if cloneExample:WaitForChild("BloodEnabled").Value == false then
			valueStore:Set(true)
			textStore:Set(first)
		else
			valueStore:Set(false)
			textStore:Set(sec)
		end
	end)
end)

Either set the Color3 into a string and revert it back, or as a table with the R, G, B colors.

local color -- color3 value

-- string
local colorString = ""
for _, propertyName in pairs("R", "G", "B") do
   colorString = colorString .. color[propertyName] .. (propertyName == "B" and "" or ", ")
end
-- now just seperate it with ", " and you get 3 values

-- table
local colorTable = {}
for _, propertyName in pairs("R", "G", "B") do
   colorTable[propertyName] = color[propertyName]
end
1 Like

ServerScriptService.bloodServer:28: invalid argument #1 to ‘pairs’ (table expected, got string)

line 28 =

Forgot to put the {} around them:

for _, propertyName in pairs({"R", "G", "B"}) do

ServerScriptService.bloodServer:29: attempt to concatenate nil with string

29 =

thanks for help btw

This basically meant you tried to do

nil .. "abcdef"
1 Like

full script:

local rs = game:GetService("ReplicatedStorage")
local data = require(game:GetService("ServerScriptService").DataStore2)
local test = require(script.test)

data.Combine("newonelol", "blood", "text")

local function convertStringToColor3(str)
	return Color3.new(table.unpack(str:split(","))); -- the values in the string are seperated by a ",", remember?
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local valueStore = data("blood",plr)
	local textStore = data("text",plr)
	
	local plrgui = plr.PlayerGui
	local settingsF = plrgui:WaitForChild("MainUI").SettingsFrame
	local cloneExample = script.Example:Clone()
	cloneExample.Parent = rs:WaitForChild("plrSettings")
	local blood = cloneExample:WaitForChild("BloodEnabled")
	local text = settingsF.BloodSet.Frame
	local firstString = "85, 170, 127"
	local SecS = "113, 1, 1"
	
	local color = firstString

	-- string
	local colorString = ""
	for _, propertyName in ipairs({"R", "G", "B"}) do
		colorString = colorString .. color[propertyName] .. (propertyName == "B" and "" or ", ")
	end
	
	local colorTable = {}
	for _, propertyName in ipairs({"R", "G", "B"}) do
		colorTable[propertyName] = color[propertyName]
	end
	
	local color2 = SecS

	-- string
	local colorString = ""
	for _, propertyName in ipairs({"R", "G", "B"}) do
		colorString = colorString .. color2[propertyName] .. (propertyName == "B" and "" or ", ")
	end

	local colorTable = {}
	for _, propertyName in ipairs({"R", "G", "B"}) do
		colorTable[propertyName] = color2[propertyName]
	end
	
	cloneExample.Name = plr.Name
	valueStore:OnUpdate(function(new)
		blood.Value = new
	end)
	textStore:OnUpdate(function(new)
		text.BackgroundColor3 = new
	end)
	settingsF.BloodSetButton.MouseButton1Click:Connect(function()
		if cloneExample:WaitForChild("BloodEnabled").Value == false then
			valueStore:Set(true)
			textStore:Set(color)
		else
			valueStore:Set(false)
			textStore:Set(color2)
		end
	end)
end)

Wait I just realised I complicated everything. You can just use:

-- when saving:
local hexCode = color:toHex() 
saveDatastore(hexCode)

-- when loading:
local hexData = loadDatastore()
Color3.fromHex(hexData) 
1 Like

what is hexdata means from my script cause i did not understand it

With hexData I am refering to the hexCode when it gets loaded.

1 Like

so i put it like that not touch anything else? put like if yes

1 Like

it not works

ServerScriptService.bloodServer:23: attempt to call missing method ‘toHex’ of string

no no i did it thanks for help

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