Problem Saving / Loading Datastore (Table Datastore)

  1. What do you want to achieve?
    I’m creating a character creation system in-game but I have so many values I don’t want to have a different datastore code for all of them and decided to figure out how to save em all as in a table. (JSON)

  2. What is the issue?
    I can’t get it to save / load or both.

  3. What solutions have you tried so far?
    I’ve followed several devforum tutorials but I just can’t seem to get it to work.

Player Joined:

	local characterdata = {}
	
	local successCharacter, ErrorCharacter = pcall(function()
		
		game:GetService("DataStoreService"):GetDataStore("CharacterDatastoreTest_1" .. player.UserId):GetAsync(player.UserId)
		
	end)
	
	if successCharacter then
		
		characterdata = game:GetService("HttpService"):JSONDecode(characterdata)
		
		if characterdata.FirstName ~= nil then
			
			print("Found Name in table")
			
			firstname.Value = characterdata.FirstName
			
		end
		
	else
		
		warn(ErrorCharacter)
		
	end

Player Removing:

	local characterdata = {}
	
	for i, v in pairs(player.CharacterStats.Character:GetChildren()) do
		
		characterdata[v.Name] = v.Value
		
	end
	
	local successCharacter, ErrorCharacter = pcall(function()

		game:GetService("DataStoreService"):GetDataStore("CharacterDatastoreTest_1" .. player.UserId):SetAsync(player.UserId, game:GetService("HttpService"):JSONEncode(characterdata))

	end)
	
	if successCharacter then
		
		print("Table saved")
		
	else
		
		warn(ErrorCharacter)
		
	end

If you know how to fix it reply down below.

you dont have json it for it to work
pass the table directly in
also you dont need an new datastore for every player

1 Like

Wait hold on, I did this and my datastore suddenly worked, is there something wrong or is this alright, I never work with tables or JSON I don’t really know

characterdata = game:GetService("DataStoreService"):GetDataStore("CharacterDatastoreTest_1" .. player.UserId):GetAsync(player.UserId)

prevously I just put the datastore, now I added the variable to it

I got it to work now but I don’t feel like it’s 100% correct, if y’all want to correct me once more just drop a reply down below, appreciate it

try

local dataservice = GetService('DataStoreService')--edited
local datastore = dataservice:GetDataStore('PlayerData')--edited
game.Players.PlayerAdded:Connect(function(player)--edited
	local characterdata = {}
	
	local successCharacter, ErrorCharacter = pcall(function()
		
		datastore :GetAsync(player.UserId)--edited
		
	end)
	
	if successCharacter then
		
		characterdata = game:GetService("HttpService"):JSONDecode(characterdata)
		
		if characterdata.FirstName ~= nil then
			
			print("Found Name in table")
			
			firstname.Value = characterdata.FirstName
			
		end
		
	else
		
		warn(ErrorCharacter)
		
	end
end)--edited
game.Players.PlayerRemoving:Connect(function(player)--edited
	local characterdata = {}
	
	for i, v in pairs(player.CharacterStats.Character:GetChildren()) do
		
		characterdata[v.Name] = v.Value
		
	end
	
	local successCharacter, ErrorCharacter = pcall(function()

		datastore :SetAsync(player.UserId,characterdata)--edited

	end)
	
	if successCharacter then
		
		print("Table saved")
		
	else
		
		warn(ErrorCharacter)
		
	end
end)--edited

if you use this then all players lose their data

1 Like

Pretty much same code, you just abbreviated it, I don’t prefer that way but appreciate it

Are you using game:BindToClose()?

?? never heard of that, what’s that

Sorry for the late response -

Try doing this,

local function saveData(player)
   --add your save script
end)

game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do  
		saveData(player)
	end
end)
1 Like

Is that when the server is shutting down right, without that the data will not save, right?

Yea, without it the data won’t save if the player gets kicked out from the server shutting down

1 Like

I got a question, will the BindToClose() function run if you normally close the game, if not then this is right?
image

If you normally close the game, the players.PlayerRemoving function will run instead.

1 Like

Alright, fixed up the code, thank you for mentioning, because I heard people lost data when shutdowns occured, now I now what this functions is for, glad I learned something new.

1 Like

Hold on quick bump, I don’t know if you know this, but whenever a new player joins the game and his data is new it gives me an error “argument missing” something like that, it doesn’t happen after, is there a way to avoid that, if not it’s fine

image

Nevermind I got it, i forgot to add warn()