My leaderstats does not work

[My datastore/leaderstats does not work i need help with fixing this]

local datastore = game:GetService("DataStoreService"):GetDataStore("data")

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

	local Gold = Instance.new("NumberValue")
	Gold.Parent = leaderstats
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user-" .. player.userId

	local storeditems = datastore:GetAsync(key)

	if storeditems then
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
	else
		local items = {Gold.Value, Rebirths.Value}
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value, player.leaderstats.Rebirths.Value}
	local key = "user-" .. player.userId

	datastore:SetAsync(key, items)
end)

First off, type in local Players = game:GetService('Players') under local datastore = game:GetService("DataStoreService"):GetDataStore("data")

Then, replace game.Players.PlayerAdded:Connect(function(player) with Players.PlayerAdded:Connect(function(player)
(just get rid of game)

It should work now, please tell me if it did!

I found a typo that should be player.UserId

It’s highly recommended that you encase your DataStore functions inside pcalls, so that you properly know what Outputs back & you can see the results it returned back

local datastore = game:GetService("DataStoreService"):GetDataStore("data")

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

	local Gold = Instance.new("NumberValue")
	Gold.Parent = leaderstats
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user-" .. player.UserId
	
	local storeditems
	local success, whoops = pcall(function()
		storeditems = datastore:GetAsync(key)
	end)

	if success and storeditems then
		print("Data loaded successfully!")
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
	else
		warn(whoops)
		Gold.Value = 0
		Rebirths.Value = 0
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value, player.leaderstats.Rebirths.Value}
	local key = "user-" .. player.UserId
	
	local success, whoops = pcall(function()
		datastore:SetAsync(key, items)
	end)
	
	if success then
		print("Data saved successfully!")
	else
		warn(whoops)
	end
end)

Also double check to make sure that API Services in your Game Settings are Enabled