IntValue in workspace not saving?

Hello! I have a value in workspace which i want load once when the server starts and save when the server closes. But it doesn’t get saved for some reason.

local DS = game:GetService("DataStoreService"):GetDataStore("Left")
local Check = false
game.Players.PlayerAdded:Connect(function(plr)
	if Check == false then
		Check = true
		task.wait()
		local plrkey = "id"..plr.userId
		local save1 = game.Workspace.Screen.SurfaceGui.Background.Left

		local GetSaved = DS:GetAsync(plrkey)
		if GetSaved then
			save1.Value = GetSaved[1]
		else
			local NumbersForSaving = {
				save1.Value,
			}
			DS:SetAsync(plrkey, NumbersForSaving)
		end
	end
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		DS:SetAsync("id"..plr.userId, {
			game.Workspace.Screen.SurfaceGui.Background.Left.Value
		})
	end
end)

You cannot store a table in the DataStoreService.
Convert the table into a JSON string first, then store it.

storing tables in datastoreservice works perfectly fine for me? i have a table with more tables, numbers, and strings inside of it and ive never had to JSON encode it

i’ve done multiple data stores which had tables in them, as @svenska_kartongbit already said, they worked perfectly fine

My bad about that.


1 Like

show me the output so i can see what its saying

My bad, i forgot to write the error.
ServerScriptService.LeftSave:12: attempt to index number with number

You’re using first player’s userid to load and save the data (and saves new data if there no data) and then BindToClose just saves loaded value to all players. lets say there was player that never played and it joins first then this means new data will be saved to him and loaded to server, if server closes it overwrites that old data other players have, with new reseted data since its first loaded.

If this wasn’t helpfull please provide me with more information about what is this code for, if its just for saving value that shared across all servers I recommend checking out MessagingService and saving that data with constant key value.

extra; SetAsync and GetAsync can give error, I recommend wrapping them with pcall, and handling errors with some backup code.

Thank you for your detailed explanation! Regarding your question yes, this code is just for saving a value shared across all servers. I know about messaging service but I wanted to set that up after saving the value.
What is a constant key value?

A constant is a value that cannot be altered by the program during normal execution , i.e., the value is constant.

instead of using, userId u can just use pre-determined value, like calling it “ItemsLeft”. Also u don’t need playerAdded event, all scripts start once server start.

1 Like

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