StringValue not saving

Hello.

I want the values of the strings to save when you leave and when you rejoin there same as you left them. I can’t seem to get the saving function to work I get no errors. I tried multiple videos and looked up some stuff to try to make it work and tested it.

local DSS = game:GetService("DataStoreService"):GetDataStore("crazystuff324423432")

function save(plr)
	
	local s = {}
	
	for i, v in pairs(plr:WaitForChild("Data"):GetChildren())do
		
		s[i] = {[v.Name] = v.Value}
		
	end	
	
	DSS:SetAsync(plr.UserId, s)
	
end

game.Players.PlayerAdded:Connect(function(Player)
	
	local folder = Instance.new("Folder", Player)
	folder.Name = "Data"
	
	local hair = Instance.new("StringValue", folder)
	hair.Name = "Hair"
	hair.Value = "Hair1"
	
	local success, errorm = pcall(function()
		
		local loaded = DSS:GetAsync(Player.UserId)
		
		if loaded then
			
			print(loaded)
			
			for i, v in pairs(loaded)do
				
				print(i)
				
			end
			
		end
		
	end)
	
end)

game:BindToClose(function()
	for i, v in pairs(game.Players:GetChildren())do
		save(v)
	end
end)

I always check on the server when dealing with DataStore or MemoryStoreService:

Try changing from
aad4e50e1ae1195e5417a35726c9cf0baf339e99
to

86d28cb84ca8bba3ef01d7cc83c78e53e98a15b8
and then run your test maybe you’ll see errors or some prints!

I got nothing, it still seems to have no errors.

You should only wrap the function that handled the network call with protective calls, not this entire code.

Secondly, you’re saving your data in a nested table form, which means you’re using a table to store some data in another table like so:

local nested = {
        { "Hair" = "Hair1"},
        ……
}

And when you attempt to print those data:

The v variable would still be a table. So either you change your way to load/print the data or you change the way you save the data.

Potential problems:

  • It doesn’t look like your code has any sections that add the loaded data to the StringValues.
  • You wrap your entire piece of code in a pcall to hide all the error messages. If you ever do that, make sure to either handle the error, print the error, or be certain all possible errors are fine.
  • I think the problem is that you input Player.UserId into GetAsync, but GetAsync takes a string while UserId is an int/number.
  • You also should save data when players leave the game, not just when the server ends.

Edit:

The print function prints tables pretty nicely into the output, I don’t think this needs to be changed. Would definitely be neater to just use string keys instead though (@OP don’t use string keys and number keys though, that will cause string keys to be ignored).

Try saving the points when the player leaves the game, so you should try yo setup a event like this:

local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(player)
	save(player)
end)

Hope this would work :slight_smile:

2 Likes

I’m not sure if this is the issue but you should probably be using HttpService:JSONEncode(s) and HttpService:JSONDecode(loaded).
https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONEncode
https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONDecode

1 Like