Datastore returning nil

Are you sure it doesn’t work? I tried it and it works perfect for me.

it doesn’t save my data nor does it output anything

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 or 0
		Gems.Value = data.Gems or 0
		Multi.Value = data.Multi or 1
		TClicks.Value = data["Total Clicks"] or 0
	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 to move data_table outside of pcall.

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

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

It says “Data successfully saved” when i exit, but when i go back into the game it doesn’t load (doesn’t output anything either)

I have no idea why it doesn’t load. Maybe try changing this:

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

On this:

Clicks.Value = data.Clicks or 0
Gems.Value = data.Gems or 0
Multi.Value = data.Multi or 1
TClicks.Value = data["Total Clicks"] or 0

literally no difference made, still doesnt load

Alr so it prints Data successfully saved but it doesn’t actually save.
image

1 Like

I used this to test and worked fine for me.

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)

	Clicks.Value = data.Clicks or 0
	Gems.Value = data.Gems or 0
	Multi.Value = data.Multi or 1
	TClicks.Value = data["Total Clicks"] or 0
end)

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

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

	local success, errormessage = pcall(function()
		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)

it says it saves the data but it doesn’t…

As I remember, all those were in the “leaderstats” folder. The path you defined takes those values from the player instance, not from the leaderstats folder.

As I said in the original thread. All the parenting is correct. Only 1 is meant to be in leaderstats.

Maybe try to to wait for those values.
player:WaitForChild("ValueName").Value

Have you read the most updated code in the comments? That’d be the most recent - a lot has changed since the main thread.

In the latest code you used,

You still don’t wait for those values, even if you add them to a table. You define a direct path which sometimes returns nil when the server doesn’t have time to find the values.

Or at least I didn’t see where you waited for those values

done that, still doesn’t work but yeah

Works perfectly good for me. Try changing the DataStore key? Instead of “-stats” name it “-stats2” or something. Or somehow clear the already saved data.

Resolved - outside sources. Thanks to everyone for the help.