Leaderstats doesn't work at all, and it's so wierd!

Basically I’m making a game like cookie clicker.
and I have this script right here to make leaderstats. But it doesn’t work proprely…

-- Services --
local DataService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

-- Variables --
local myDataStore = DataService:GetDataStore("myDataStore")

-- Events --
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 = leaderstats

	local Multiplier = Instance.new("IntValue")
	Multiplier.Name = "Multiplier"
	Multiplier.Parent = leaderstats
	Multiplier.Value = 1 -- Doesn't change.

     -- It does make these instances though...

	local playerUserId = "Player_"..player.UserId

	-- Getting the data.
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	if success then
		Clicks.Value = data.Clicks
		Multiplier.Value = data.Multiplier 
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		Wood = player.leaderstats.Clicks.Value;
		Multiplier = player.leaderstats.Multiplier.Value;
	}

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data) -- Doesn't set the data
	end)
	if success then
		print("Data saved!") -- Prints but it still doesn't save...
	else
		warn("Data wasn't saved!")
	end
end)

Help is appreciated!

You are trying to access data that has been saved differently when writing to the datastore. You have saved the data under “Wood” instead of Clicks which will return nil. I’d suggest using else statements to ensure that you know when something errors in a pcall or if you have a success.

1 Like

I have managed to fix it on my own…