Datastore returning nil

Here’s my code:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = player
	Clicks.Value = 0

	local Gems = Instance.new("NumberValue")
	Gems.Name = "Gems"
	Gems.Parent = player
	Gems.Value = 0

	local Multi = Instance.new("NumberValue")
	Multi.Name = "Multi"
	Multi.Parent = player
	Multi.Value = 1

	local TClicks = Instance.new("IntValue")
	TClicks.Name = "Total Clicks"
	TClicks.Parent = leaderstats
	TClicks.Value = 0

	local data = nil
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.userId.."-stats") 
	end)

	if success and data ~= nil then 
		Clicks.Value = data
		Gems.Value = data
		Multi.Value = data
		TClicks.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		playerData:SetAsync(player.userId.."-stats", player.leaderstats["Total Clicks"].Value, player.Clicks.Value, player.Gems.Value, player.Multi.Value)
	end)

	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end

end)

This code keeps returning “nil” as the error message.

image

image
Not quite sure why it keeps doing this, and yes, all the parentings are correct.

playerData:SetAsync(player.userId.."-stats", player.leaderstats["Total Clicks"].Value, player.Clicks.Value, player.Gems.Value, player.Multi.Value)

instead do it one by one like:

playerData:SetAsync(Key, data)
playerData:SetAsync(Key1, data1)

I think data stores cannot hold multiple values or something

you could try putting everything after the first parameter in {}

datastores tend to only take in a key and a value. the value is often a list/dictionary of other values.

It returns nil because the Script dont know wich data is for wich value

Instead of:

	if success and data ~= nil then 
		Clicks.Value = data
		Gems.Value = data
		Multi.Value = data
		TClicks.Value = data

do:

	if success and data ~= nil then 
		Clicks.Value = data[Clicks.Name]
		Gems.Value = data[Gems.Name]
		Multi.Value = data[Multi.Name]
		TClicks.Value = data[TClicks.Name]

to get the name.

You can also do:

	if Data then
		for i, Stats in pairs(stats:GetChildren()) do
			Stats.Value = Data[Stats.Name]
		end				
	end

still does return ‘nil’ though.

still doesn’t work, tried it several ways

Change this:

playerData:SetAsync(player.userId.."-stats", player.leaderstats["Total Clicks"].Value, player.Clicks.Value, player.Gems.Value, player.Multi.Value)

to this:

local data_table = {
["Total Clicks"] = player.leaderstats["Total Clicks"].Value;
["Clicks"] = player.Clicks.Value;
["Gems"] = player.Gems.Value;
["Multi"] = player.Multi.Value;

playerData:SetAsync(player.userId.."-stats", data_table)

And when you’re getting data:

if success and data ~= nil then 
		Clicks.Value = data.Clicks
		Gems.Value = data.Gems
		Multi.Value = data.Multi
		TClicks.Value = data["Total Clicks"]
	else
		print("Error while getting your data")
		warn(errormessage)
	end

The problem problem may be that the client connects before the execution of the script.

Try creating a function and then calling it for each player that is already playing.

function playerAdd(player)
	...
end

game.Players.PlayerAdded:Connect(playerAdd)

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

for p in pairs(game.Players:GetPlayers()) do
	playerAdd(p)
end

still returns nil, i don’t even know anymore lol

Okay then maybe try:

local data

instead of:

local data = nil

now theres just no output of the console, data still doesn’t save.

I forgot to close a table. Try this:

local data_table = {
["Total Clicks"] = player.leaderstats["Total Clicks"].Value;
["Clicks"] = player.Clicks.Value;
["Gems"] = player.Gems.Value;
["Multi"] = player.Multi.Value;
}

playerData:SetAsync(player.userId.."-stats", data_table)

Do you even have the API settings enabled?

for Creazione: Yyes i have it enabled

for FunAsiK: i already closed the table as soon as u posted, i noticed instantly

Updated code:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = player
	Clicks.Value = 0

	local Gems = Instance.new("NumberValue")
	Gems.Name = "Gems"
	Gems.Parent = player
	Gems.Value = 0

	local Multi = Instance.new("NumberValue")
	Multi.Name = "Multi"
	Multi.Parent = player
	Multi.Value = 1

	local TClicks = Instance.new("IntValue")
	TClicks.Name = "Total Clicks"
	TClicks.Parent = leaderstats
	TClicks.Value = 0

	local data
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.userId.."-stats") 
	end)

	if success and data ~= nil then 
		Clicks.Value = data.Clicks
		Gems.Value = data.Gems
		Multi.Value = data.Multi
		TClicks.Value = data["Total Clicks"]
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		local data_table = {
		["Total Clicks"] = player.leaderstats["Total Clicks"].Value;
		["Clicks"] = player.Clicks.Value;
		["Gems"] = player.Gems.Value;
		["Multi"] = player.Multi.Value;
		}
		playerData:SetAsync(player.userId.."-stats", data_table)
	end)

	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end

end)

Try parenting everything at the end of the function

not all values parent to leaderstats

Try to change:

player.userId.."-stats"

To

"-stats" .. player.userId

Your issue is most likely that you are trying :GetAsync() when the player is first logged into the game, so it returns nil.

if data then 
	Clicks.Value = data.Clicks or 0
	Gems.Value = data.Gems or 0
	Multi.Value = data.Multi or 0
	TClicks.Value = data["Total Clicks"] or 0
    -- we're setting it to 0 in case if player joined for the first time.
end

This does change nothing, thats just like a password

still doesnt work lmao, typing this extra bit here for 30 letters