Global leaderboard won't work?

What do you want to achieve? I want to make a global leaderboard. I used this script:

local DataStoreService = game:GetService("DataStoreService")
local KillsLeaderboard = DataStoreService:GetOrderedDataStore("KillsLeaderboard")

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = KillsLeaderboard:GetSortedAsync(false, 5)
		local KillsPage = Data:GetCurrentPage()
		for rank, data in ipairs (KillsPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Kills = data.value
			local isOnLeaderboard = false 
			for i, v in pairs(game.Workspace.InfoSign.Leaderboard.LeaderboardGui.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
			
			if Kills and IsOnLeaderboard == false then
				local NewLeaderboardFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				NewLeaderboardFrame.Player.Text = Name
				NewLeaderboardFrame.Kills.Text = Kills
				NewLeaderboardFrame.Rank.Text = "#"..Rank
				NewLeaderboardFrame.Position = Udim2.new(0, 0, NewLeaderboardFrame.Position.Y.Scale + (.08 * #game.Workspace.InfoSign.LeaderboardGui.SurfaceGui.Holder:GetChildren()), 0)
				NewLeaderboardFrame.Parent = game.Workspace.InfoSign.Leaderboard.LeaderboardGui.Holder
			end
		end
	end)
	
	if not success then
		print(errorMessage)
	end
end

while true do
	
	for_, player = pairs(game.Players:GetPlayers()) do
		KillsLeaderboard:SetAsync(player.UserId, player.leaderstats.kills.Value)
	end
	
	for _, frame in pairs(game.Workspace.InfoSign.Leaderboard.LeaderboardGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print ("Updated!")
	
	wait (60)
end

What is the issue? it keeps showing me this error:

ServerScriptService.LeaderboardHandler:39: attempt to index nil with ‘kills’

and links this line:

	KillsLeaderboard:SetAsync(player.UserId, player.leaderstats.kills.Value)

What solutions have you tried so far? I thought that I maybe capitalized it wrong, but this was not the case. I thought that I maybe broke the parenting stuff but that’s not the case. I thought i used a wrong type of a local variable but nah. Help me?

By the way, this is the parenting:

Bez tytułudg

Bez tytułufg

and here’s the leaderstats script:

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

Can we see the leaderstats script?

Sure!

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

It is a capital K and you probably need to do kills.Value in your leaderstats script.

1 Like

Same error, just this time it’s “Kills” instead of “kills”

Can you go into studio, then click join game, and go to explorer → players → yourplayer → leaderstats and click on the kills value and see if it is there?

1 Like

Hmm, it’s not there…

jk

Can you try:

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

for _, player in ipairs(game.Players:GetPlayers() do
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
end

This way, you’ll be sure that leaderstats is created and parented to new players and to current players.

1 Like

ServerScriptService.LeaderboardHandler:13: Expected ‘)’ (to close ‘(’ at column 24), got ‘do’

for _, player in ipairs(game.Players:GetPlayers() do

for_, player = pairs(game.Players:GetPlayers()) do

This could just be a typo but I noticed that there isn’t a space after for.

1 Like

It works in a local leaderboard but not in the global one. It probably has to do something with how big the leaderboard is.
Bez tytułuffp

KillsLeaderboard:SetAsync(player.UserId, player.leaderstats.kills.Value)

Is kills supposed to be lowercase here?

1 Like

I already tried making it be capitalized, I got the same error but with “kills” being capitalized.

ServerScriptService.LeaderboardHandler:39: attempt to index nil with ‘kills’

ServerScriptService.LeaderboardHandler:39: attempt to index nil with ‘Kills’

for_, player = pairs(game.Players:GetPlayers()) do

Replace the = with in and add a space after for.

local Kills = data.value

value should be capitalized.

1 Like

i replaced it but it still wouldn’t work.

I changed it but now i have an error called “HTTP 403 (Forbidden)” for some reason?

Try setting the place access from private to public and restarting studio. I read a different thread and someone said that it works.