DataStore not working... no idea why

Hey everyone! I’m working on a computer system, where the player can customize it, for example, changing the “OS” color. I’m trying to save these changes that the player does to the system, but I cannot make it work. No errors are displayed on output, which makes me very lost, as it is my first time ever using datastores.

Here’s my workflow:

First of all, the ColorValue (actually a StringValue) on the client receives the new color name, and fires it to the server’s StringValue of the same name: (At PlayerGui)

ColorValue.Changed:Connect(function()
	
	game.ReplicatedStorage.Remotes.ValueChanger:FireServer(game.ReplicatedStorage.Datastore.BobOS_Settings.Color, ColorValue.Value)
	
	local color = ColorValue:FindFirstChild(ColorValue.Value)
	for i, v in ipairs(ColorObjects) do
		v.ImageColor3 = color.Value
		
	end
end)

Until now, everything seems to work, the server ColorValue.Value seems to change accordingly to the client one
image

But here comes the datastoring part: (At ServerScriptService)

From here, I’m completely blinded and have no idea what the problem is, as even the print(ColorValue.Value) won’t work.

local ColorValue = script.Color

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

--// ColorValue Player
local function saveColorValue()
	local success, error = pcall(function()
		ColorDataStore:SetAsync("ColorValue", ColorValue.Value)
	end)
	if not success then
		print("Error saving ColorValue to data store: " .. error)
	end
end

local function loadColorValue()
	local success, value = pcall(function()
		return ColorDataStore:GetAsync("ColorValue")
	end)
	if success then
		ColorValue.Value = value
	else
		print("Error loading ColorValue from data store: " .. value)
	end
end

ColorValue.Changed:Connect(saveColorValue)
ColorValue.Changed:Connect(function()
	
	game.ReplicatedStorage.Remotes.ValueChanger.ClientReceiver:FireAllClients(game.Players.LocalPlayer.PlayerGui.BobOS.Settings.data.Color, ColorValue.Value)
end)

loadColorValue()
wait(6)
print(ColorValue.Value)

And these lines, for when the ColorValue should be loaded: (At PlayerGui)

local ClientServerReceiver = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("ValueChanger"):WaitForChild("ClientReceiver")

ClientServerReceiver.OnClientEvent:Connect(function(object, new)
	object.Value = new
end)

Sorry for the long post :smile:
But as I said, I’ve never worked with this before, so any help would be highly appreciated!

2 Likes
game.ReplicatedStorage.Remotes.ValueChanger.ClientReceiver:FireAllClients(game.Players.LocalPlayer.PlayerGui.BobOS.Settings.data.Color, ColorValue.Value)

Are you not seeing an error on this line? I thought game.Players.LocalPlayer is always nil when running on the server.

I’m not sure, I believe not, because this is sepparated from the DataStore script at the server, and the rest of this script works fine.

While the server one that should do all the saving job doesn’t even display the print(ColorValue.Value) which would help me seeing if the value is being loaded afterall

1 Like

Without running the game, you can check if the key “ColorValue” is actually set in the data store using the command line in Studio. Do you know if saveColorValue is even being called?

I didn’t even know that datastoring in studio was a thing… How could I do that?

print("Color value:", game:GetService("DataStoreService"):GetDataStore("ColorDataStore"):GetAsync("ColorValue"))

You can run this in the command line and it will print whatever is set for the “ColorValue” key in your data store.

1 Like

Thank you for the tip! It says that the DataStore is nil
image

local function saveColorValue()
	local success, error = pcall(function()
		ColorDataStore:SetAsync("ColorValue", ColorValue.Value)
	end)
	if not success then
		print("Error saving ColorValue to data store: " .. error)
	end
end

I noticed that in your pcall you make the 2nd variable error, and since error is used for printing out errors, you should make it errorMessage instead of error. It might not be the case, but thats what I noticed.

1 Like

So maybe saveColorValue isn’t being called at all. What do you do on the server when the clients fire the ValueChanger remote event?

1 Like

this was supposed to save the new color everytime the value changed:

ColorValue.Changed:Connect(saveColorValue)

Do you not have a handler for the ValueChanger remote event though? For example, ValueChanger.OnServerEvent:Connect(...)

I think not, everything that is being used to make the datastoring is in the main post

If nothing is not working I would put prints at the beginning of the script and in all of the pcalls

2 Likes

The thing is pls read on youtube tutorials on how to do datastore

Thank you for being very supportive on the Scripting Support category

I think that’s the main issue. When the clients fire the remote event to the server, there’s no code on the server that’s actually changing the value and so saveColorValue isn’t ever called.

1 Like

Turns out none of my server scripts are running, no matter what the parent is.

I’ll give it a time as it prob is an engine bug, sorry for making you waste your time, but also thanks for the effort on helping!

1 Like

No problem. Reach out if you have any more problems with this. I’m willing to help.

It is not engine bug,i am going to help u now

FIRSTLY,THIS DATASTORE IS TOTALLY WRONG.U should have a table to save into and ur script dosent???

Yes, it was! Today (one day later) the script from the original post started working!

But you’re right, the way I did everything was far from ideal, so I spent the rest of the day analyzing some other scripts and learned how to make the datastoring using a table instead of separated datastores for each value:

local DSS = game:GetService("DataStoreService")
local ComputerSettings = DSS:GetDataStore("PlayerData")

--// Load
game:GetService("Players").PlayerAdded:Connect(function(player)
	local userId = player.UserId

	local success, data = pcall(function()
		return ComputerSettings:GetAsync(tostring(userId))
	end)

	if success and data then
		script.Color.Value = data.Color
		script.Theme.Value = data.Theme
		script.Photo.Value = data.Photo
	end
	
end)

--// Save
game:GetService("Players").PlayerRemoving:Connect(function(player)
	local userId = player.UserId

	local success, errorMsg = pcall(function()
		ComputerSettings:SetAsync(tostring(userId), {
			Color = script.Color.Value,
			Theme = script.Theme.Value,
			Photo = script.Photo.Value
		})
	end)

	if not success then
		warn(errorMsg)
	end
end)