Datastore not saving string value

Hi, I am trying to save the color’s name but it isn’t being saved…? I’ve tried numerous ways to get it towork and I can’t figure it out. Here is my code:

game.Players.PlayerAdded:Connect(function(player)
	
	-- Data
	local PrimaryColor = Instance.new("StringValue")
	PrimaryColor.Name = "PrimaryColor"
	--PrimaryColor.Value = "White"
	PrimaryColor.Parent = player

local data 

	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)

	if data ~= nil then
		if data.PrimaryColor then
			PrimaryColor.Value = data.PrimaryColor
		end
end

end)
game.Players.PlayerRemoving:Connect(function(player)
	local data = {}	
	data.PrimaryColor = player.PrimaryColor.Value

	local sucess, errorMsg = pcall(function()
		DataStore:SetAsync(player.UserId, data)
	end)

	if sucess then
		print("Successfully saved")
	end
end)

Saving colors to DS can be kinda weird, instead save it as a dictionary of RGB:

-- setting
local dataToSave = {
    R = 122; -- or colorValue.Value.R
    G = 43; -- or colorValue.Value.G
    B = 255; -- or colorValue.Value.B
}

DS:SetAsync(userId, dataToSave)

-- getting

local colorDict = DS:GetAsync(userId)
local colorThatWasSaved = Color3.fromRGB(colorDict.R, colorDict.G, colorDict.B)

That’s not the issue though, the colors are already preset in variables like so:

local Blue = Color3.fromRGB(23, 15, 255)
local Cyan = Color3.fromRGB(85, 255, 255)
local Forest = Color3.fromRGB(170, 255, 0)
local Green = Color3.fromRGB(0, 158, 0)
local Lavender = Color3.fromRGB(170, 0, 127)
local Lime = Color3.fromRGB(0, 255, 0)
local Orange = Color3.fromRGB(255, 85, 0)
local Pink = Color3.fromRGB(255, 33, 255)
local Purple = Color3.fromRGB(170, 85, 255)
local Red = Color3.fromRGB(255, 0, 0)
local White = Color3.fromRGB(255, 255, 255)
local Yellow = Color3.fromRGB(255, 255, 0)

The player’s data isn’t correctly loading in.

Ok so to fix this, you will have some strange floating point errors since you use .fromRGB, but round them after:

local color = Color3.fromRGB(22, 33, 44)

local colorDict = {
    R = math.floor((color.R * 255) + 0.5);
    G = math.floor((color.G * 255) + 0.5);
    B = math.floor((color.B* 255) + 0.5);
}

I’m confused as to what this is supposed to do. I’m trying to load in the name of the color, not the RGB values. Once loaded, a remote event fires like so:

game.ReplicatedStorage.Events.SetupPrimColor.OnClientEvent:Connect(function()
	script.Parent.SettingsFrame.GameSettings.PrimaryColor.Text = "Primary Color: "..player:FindFirstChild("PrimaryColor").Value
	if player:WaitForChild("PrimaryColor").Value == "Blue" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Blue
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Blue
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Blue
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Blue
	elseif player:WaitForChild("PrimaryColor").Value == "Cyan" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Cyan
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Cyan
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Cyan
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Cyan
	elseif player:WaitForChild("PrimaryColor").Value == "Forest" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Forest
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Forest
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Forest
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Forest
	elseif player:WaitForChild("PrimaryColor").Value == "Green" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Green
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Green
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Green
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Green
	elseif player:WaitForChild("PrimaryColor").Value == "Lavender" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Lavender
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Lavender
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Lavender
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Lavender
	elseif player:WaitForChild("PrimaryColor").Value == "Lime" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Lime
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Lime
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Lime
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Lime
	elseif player:WaitForChild("PrimaryColor").Value == "Orange" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Orange
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Orange
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Orange
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Orange
	elseif player:WaitForChild("PrimaryColor").Value == "Pink" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Pink
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Pink
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Pink
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Pink
	elseif player:WaitForChild("PrimaryColor").Value == "Purple" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Purple
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Purple
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Purple
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Purple
	elseif player:WaitForChild("PrimaryColor").Value == "Red" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Red
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Red
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Red
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Red
	elseif player:WaitForChild("PrimaryColor").Value == "White" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = White
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = White
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = White
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = White
	elseif player:WaitForChild("PrimaryColor").Value == "Yellow" then
		script.Parent.ItemUI.ItemFrameSetup.Item.ItemFrame.BackgroundColor3 = Yellow
		script.Parent.MainUI.Spectate.FrontFrame.BackgroundColor3 = Yellow
		script.Parent.SettingsBtn.FrontFrame.BackgroundColor3 = Yellow
		script.Parent.ShopBtn.FrontFrame.BackgroundColor3 = Yellow
	end
end)

The primary color value isn’t changing when you load in, meaning it’s not being saved. That’s what I am trying to fix.

1 Like

In that case, just save the name of the color to the DS.

1 Like

That’s what I am trying to with my script:

game.Players.PlayerAdded:Connect(function(player)
	
	-- Data
	local PrimaryColor = Instance.new("StringValue")
	PrimaryColor.Name = "PrimaryColor"
	--PrimaryColor.Value = "White"
	PrimaryColor.Parent = player

local data 

	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)

	if data ~= nil then
		if data.PrimaryColor then
			PrimaryColor.Value = data.PrimaryColor
		end
end

end)
game.Players.PlayerRemoving:Connect(function(player)
	local data = {}	
	data.PrimaryColor = player.PrimaryColor.Value

	local sucess, errorMsg = pcall(function()
		DataStore:SetAsync(player.UserId, data)
	end)

	if sucess then
		print("Successfully saved")
	end
end)

Is there another way in doing so?

Everything should work there, are you testing this in studio? If you are, the PlayerRemoving event does not fire sometimes.

Figured out the issue, the value was being set in a local script rather than a normal script, which doesn’t save it due to client-server relationship. It now works.

1 Like

About the script I am replying to, I’d recommend creating a changeColor(colorName) function. :sweat_smile: