Table not saving in DataStore

Hello Ive been trying to make a script that saves the player’s creatures and their individual stats in a table, and then loads them in in folders. However, as Ive been trying to save this data, I always get the errorMessage. Any help is appreciated!

Also im using the name “pokemon” as a placeholder

local Players = game:GetService("Players")

local ds = game:GetService("DataStoreService"):GetDataStore("Data1")

local PlayerPokemonData = {}




game.Players.PlayerAdded:Connect(function (player)
	local party = Instance.new("Folder")
	party.Parent = player
	party.Name = "Party"
	
	--local data = ds:GetAsync(player.UserId, PlayerPokemonData)
	local sucess, errorMesage = pcall(function(player)
		local data = ds:GetAsync(player.UserId, PlayerPokemonData)
	end)
	if sucess then
		print("Data Loaded")
	else
		print("Error")
	end
for i, v in pairs(PlayerPokemonData) do
	local NewPokemon = Instance.new("Folder", party)
	NewPokemon.Name = v.PokemonName

	-- repeat this for each stat
	local atk = Instance.new("NumberValue", NewPokemon)
	atk.Name = "ATK"
	atk.Value = v.ATK
	end	
end)	


game.Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(player.Party:GetChildren()) do
		local NewPokemon = {} -- create a new table for each pokemon
		NewPokemon.PokemonName = v.Name -- Create a new value in the table that is the pokemons name (for loading pokemon)
		for i, c in pairs(v:GetChildren()) do -- Loop through the pokemons stats: ATK, DEF etc.
			NewPokemon[c.Name] = c.Value -- this will create a value in the table like: {["ATK"] = 20}
		end
		table.insert(PlayerPokemonData, NewPokemon) -- Add the new pokemon to the table were going to save
	end	
	
	--local data = ds:SetAsync(player.UserId, PlayerPokemonData)
	local sucess, errorMessage = pcall(function(player)
		local data = ds:SetAsync(player.UserId, PlayerPokemonData)
		
	end)
	
	if sucess then
		print("Data saved")
	else
		print("Error")
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetChildren()) do
		player:Kick("Server Closed")
	end
end)

What line is the error message?

Its giving the “errorMessage” in the else statement. Its printing error and not loading/saving anything

i’m pretty sure the PlayerPokemonData param is not included

1 Like

also for this one, you need to replace PlayerPokemonData with “data” instead or it’ll be simply the default table

1 Like

Hello, this helped out alot as I no longer get the error message for loading. However i am still getting an error message fir saving (the player.playerremoving function), any idea why that is happening?

I found a solution :smiley: Thank you for all your help!!!