DataStore Not working! Pls Help

HI I am busy with a datastore but I cant seem to get it working. It worked in the past before, It seems like it may be the bindtoclose.

Can Anyone help me to fix it? Please

Leaderstats - Normal Script

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local plrDataStore = require(script.Parent.plrDataStore)

local function onPlayerJoin(player)
	local loadedData = plrDataStore.getDataForPlayer(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leaderstats
	Clicks.Value = loadedData
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = leaderstats
	Rebirths.Value = loadedData
	
	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	Gems.Value = loadedData
end

local function onPlayerExit(player)
	local PlayerStats = player:FindFirstChild("leaderstats")
	local saveData = {
		Clicks = PlayerStats.Clicks.Value,
		Rebirths = PlayerStats.Rebirths.Value,
		Gems = PlayerStats.Gems.Value
	}
	plrDataStore.saveDataForPlayer(player, saveData)
end

game:BindToClose(function(player)
	local PlayerStats = player:FindFirstChild("leaderstats")
		for i,v in pairs(Players:GetPlayers()) do
			local saveData = {
				Clicks = PlayerStats.Clicks.Value,
				Rebirths = PlayerStats.Rebirths.Value,
				Gems = PlayerStats.Gems.Value
			}
			wait(5)
			plrDataStore.saveDataForPlayer(player, saveData)
	end
end)

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

DataStore - Module Script

local DataStoreService = game:GetService("DataStoreService")
local PlayerStore = DataStoreService:GetDataStore("StoreTest")

local DefaultData = {
	Clicks = 0,
	Rebirths = 0,
	Gems = 0
}

local PlayerDataStore = {}

function PlayerDataStore.getDataForPlayer(player)
	
	local PlayerData
	
	local Success, err = pcall(function()
		PlayerData = PlayerStore:GetAsync(player.UserId)
	end)
	if not Success then
		print(err)
		PlayerData = DefaultData
	else
		print("Found Data")
	end
	return PlayerData
end

function PlayerDataStore.saveDataForPlayer(player, saveData)
	local success, err = pcall(function()
		PlayerStore:SetAsync(player.UserId, saveData)
	end)
	
	if not success then
		print(err)
		print("Something went wrong cant save data!")
	end
end

return PlayerDataStore

Thank You! Much appreciated.

ServerScriptService.stats:39: attempt to index nil with ‘FindFirstChild’

Error is at the bind to close.

local PlayerStats = player:FindFirstChild("leaderstats")

The script itself seems to be very all over the place,

In your BindToClose() Function you are passing player along with it.
BindToClose does not pass that variable so it will error, If you want to save data for each player you should do so by using for i,v in pairs() and game:GetService("Players"):GetPlayers().

You are going to get an error of attempting to send too many requests for the last person who leaves the game.

Since when it’s the last person who leaves, the player removing saves the data, and then the bind to close tries to save the data, but you just saved data, and now you are doing it too much.