Tables are not saving

What line is giving you that error? There’s also a lot of misspelling there, is that in the original code? Also you’re saving the data as a dictionary, then reading from it as if it were an array.

this is my og code, how would i get as a dictionary.

Intro to Dictionaries

@MightyDantheman

i read it and this this code, but it still gives the same error.

local save = {}

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("datamaSnef")
local players = game:GetService("Players")



function save.playeraddede(player)

	local data = ds:GetAsync(player.UserId)
	
	player.leaderstats.Coins.Value = data["Coins"]
	player.leaderstats.Strength.Value = data["Strength"]
	player.leaderstats.Gems.Value = data["Gems"]

	
end

game:BindToClose(function(plr)
	wait(2)
	pcall(function()

		local saveData = {
			Coins = plr.leaderstats.Coins.Value,
			Strength = plr.leaderstats.Strength.Value,
			Gems = plr.leaderstats.Gems.Value
		}
		local httpService = game:GetService('HttpService')
		ds:SetAsync(plr.UserId,httpService:JSONEncode(saveData))
	end)
end)
return save

You still need to decode the json when loading the data. Here’s your code with it updated:

local save = {}

local httpService = game:GetService('HttpService')
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("datamaSnef")
local players = game:GetService("Players")



function save.playeraddede(player)

	local data = httpService:JSONDecode(ds:GetAsync(player.UserId))

	player.leaderstats.Coins.Value = data["Coins"]
	player.leaderstats.Strength.Value = data["Strength"]
	player.leaderstats.Gems.Value = data["Gems"]


end

game:BindToClose(function(plr)
	task.wait(2)
	pcall(function()

		local saveData = {
			Coins = plr.leaderstats.Coins.Value,
			Strength = plr.leaderstats.Strength.Value,
			Gems = plr.leaderstats.Gems.Value
		}
		ds:SetAsync(plr.UserId,httpService:JSONEncode(saveData))
	end)
end)
return save

it still gives me

 Argument 1 missing or nil 

at

local data = httpService:JSONDecode(ds:GetAsync(player.UserId))

The only way that can be is if “player” is nil. Check your player added function to see what’s being put through.

yes, now it the userId is nil, how can i fix it.

and when i only print player it prints:

  ▼  {
                    ["playeraddede"] = "function"
                 }  -  Server - saveandloaddata:11

I’m very confused on what you’ve got going on. Just use the normal PlayerAdded event:

local players = game:GetService('Players')
players.PlayerAdded:Connect(function(player)
	print(player.Name)
	-- do stuff
end)

hello, it prints my userId but it still says

Argument 1 missing or nil 

in this line:

local data = httpService:JSONDecode(ds:GetAsync(player))
local httpService = game:GetService('HttpService')
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("datamaSnef")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local data = ds:GetAsync(player.UserId)
	if data then
		data = httpService:JSONDecode()
		player.leaderstats.Coins.Value = data["Coins"]
		player.leaderstats.Strength.Value = data["Strength"]
		player.leaderstats.Gems.Value = data["Gems"]
	end
end)

game:BindToClose(function(plr)
	pcall(function()
		local saveData = {
			Coins = plr.leaderstats.Coins.Value,
			Strength = plr.leaderstats.Strength.Value,
			Gems = plr.leaderstats.Gems.Value
		}
		ds:SetAsync(plr.UserId,httpService:JSONEncode(saveData))
	end)
end)

now it gives nothing. No errors , no warnings but the data still doesnt save / load

I was expecting you to have more code outside of what you posted.

local httpService = game:GetService('HttpService')
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("datamaSnef")
local players = game:GetService("Players")

local loadedPlayers = {}

players.PlayerAdded:Connect(function(player)
	local data = ds:GetAsync(player.UserId)
	if data then
		data = httpService:JSONDecode()
		player.leaderstats.Coins.Value = data.Coins
		player.leaderstats.Strength.Value = data.Strength
		player.leaderstats.Gems.Value = data.Gems
	end
	table.insert(loadedPlayers,player)
end)

players.PlayerRemoving:Connect(function(player)
	local found = table.find(loadedPlayers,player)
	if found then
		table.remove(loadedPlayers,found)
		local saveData = {}
		saveData.Coins = player.leaderstats.Coins.Value
		saveData.Strength = player.leaderstats.Strength.Value
		saveData.Gems = player.leaderstats.Gems.Value
		local function attemptSave(i)
			i = i or 0
			i+=1
			if not pcall(function()
					ds:SetAsync(player.UserId,httpService:JSONEncode(saveData))
				end) then
				if i <= 10 then
					task.wait(1)
					attemptSave()
				end
			end
		end
		attemptSave()
	end
end)

In the future, please try to figure things out on your own. Having others write your code isn’t anywhere near as helpful as you learning how to do it yourself.

Anyways, the above code is the most basic way to save data. However, it’s missing things such as data locking, as to avoid mixing data versions due to rejoining too quickly. At the very least, it will prevent nil data from being saved.

th\ Sorry i couldnt figure out myself as im new to datastores :frowning:

but how can i add more tables? like i have a folder for the player. Ownedstuff for instance, how can i save it?

You can add them to the table like you have with your other values. Although you’ll want to adjust the loading function to adapt for unexpected data. I’ve updated the code so that loading will handle any exception regarding new or removed data:

local httpService = game:GetService('HttpService')
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("datamaSnef")
local players = game:GetService("Players")

local loadedPlayers = {}

players.PlayerAdded:Connect(function(player)
	local data = ds:GetAsync(player.UserId)
	if data then
		data = httpService:JSONDecode()
		for key,value in pairs(data) do
			pcall(function()
				player.leaderstats[key].Value = value
			end)
		end
	end
	table.insert(loadedPlayers,player)
end)

players.PlayerRemoving:Connect(function(player)
	local found = table.find(loadedPlayers,player)
	if found then
		table.remove(loadedPlayers,found)
		local saveData = {}
		saveData.Coins = player.leaderstats.Coins.Value
		saveData.Strength = player.leaderstats.Strength.Value
		saveData.Gems = player.leaderstats.Gems.Value
		local function attemptSave(i)
			i = i or 0
			i+=1
			if not pcall(function()
					ds:SetAsync(player.UserId,httpService:JSONEncode(saveData))
				end) then
				if i <= 10 then
					task.wait(1)
					attemptSave()
				end
			end
		end
		attemptSave()
	end
end)

If you want the save to automatically collect all leaderstats and save it without you having to manually add or remove them, you can do that too with this version:

local httpService = game:GetService('HttpService')
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("datamaSnef")
local players = game:GetService("Players")

local loadedPlayers = {}

players.PlayerAdded:Connect(function(player)
	local data = ds:GetAsync(player.UserId)
	if data then
		data = httpService:JSONDecode()
		for key,value in pairs(data) do
			pcall(function()
				player.leaderstats[key].Value = value
			end)
		end
	end
	table.insert(loadedPlayers,player)
end)

players.PlayerRemoving:Connect(function(player)
	local found = table.find(loadedPlayers,player)
	if found then
		table.remove(loadedPlayers,found)
		local saveData = {}
		for _,value in pairs(player.leaderstats:GetChildren()) do
			saveData[value.Name] = value.Value
		end
		local function attemptSave(i)
			i = i or 0
			i+=1
			if not pcall(function()
					ds:SetAsync(player.UserId,httpService:JSONEncode(saveData))
				end) then
				if i <= 10 then
					task.wait(1)
					attemptSave()
				end
			end
		end
		attemptSave()
	end
end)

Just note that this version is only compatible with updating leaderstats automatically. You can add if statements or other logic to make this work with other types of data besides just leaderstats, but this won’t do that in its base form.

ik how to do that, but how can i save shops? like lets say a player owns an item which he/she unlocked. But want it to be saves. How can i do that?

here is a video guide on how to save and load player data

You’re saving and loading a table. You’d handle it the same way you would a table. Just remember that if you’re adding new data, you should update your load function to handle missing data (as if the player’s save data is older, it might not have things that newer data would).

SetAsync is not a good approach to save data in Roblox, I’d recommend using UpdateAsync, for me, it has never given any problems