Local data tables gives nil?

Hello, the issue is simple, i tried doing a datastore script using table with character values, but using print, it always return the data as nil

I already tried: Using attributes, waitforchild in both character and values, using folders and without folders, using instance.new().

local datasservice = game:GetService("DataStoreService")
local datastore1 = datasservice:GetDataStore("datastore1")

--
game.Players.PlayerRemoving:Connect(function(player)
	local playerkey = "player:".. player.UserId
	local char = player.Character or player.CharacterAdded:Wait()
	local data = {
		atk = char.stats.atk;
		def =  char.stats.def;
		dex =  char.stats.dex;
		mana =  char.stats.mana;
		manamax = char.stats.manamax;
		vit =  char.stats.vit;
		wis =  char.stats.wis;
	}
	--
	local success, errormessage = pcall(function()
		datastore1:SetAsync(playerkey, data)
	end)
	if success then
		print(player.Name.. "'s data successfully saved")
	else
		warn(errormessage)
	end
end)
--
game.Players.PlayerAdded:Connect(function(player)
	local playerkey = "player:".. player.UserId
	local char = player.Character or player.CharacterAdded:Wait()
	--
	local success, errormessage, data = pcall(function()
		return datastore1:GetAsync(playerkey)
	end)
	print('typeofchar:'.. typeof(char))
	print(data)
	if success then
		char.stats.atk = data.atk
		char.stats.def = data.def
		char.stats.dex = data.dex
		char.stats.mana = data.mana
		char.stats.manamax = data.manamax
		char.stats.vit = data.vit
		char.stats.wis = data.wis
		char.Humanoid.Health = data.health
		char.Humanoid.MaxHealth = data.healthmax
		print("data successfully loaded")
	else
		warn(errormessage)
	end
end)


Here is the error:
image

there is the folder:
image

and here is nil: (print(data))
image

It is stored in the Character, which is the first to be removed.

Store the player data in ReplicatedStorage, that would overcome any issues related the player leaving.

This is how I store my data
image

Each player has folder in PlayerData, which is a folder in ReplicatedStorage. Then the player’s folder has the stats.

1 Like

Hey, thanks, but even while using ReplicatedStorage to store values, it does the same error

If I am right this is caused by the server closing.
Please read the following article:

1 Like