I can't figure out how to make a leaderboard!

I am trying to make a leaderboard where players get 5 points each hour. Here are the scripts:

This one sets up the leaderboard.

local leaderboardSetup = {}

local Players = game:GetService("Players")

function leaderboardSetup.leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)

return leaderboardSetup

This one is a module script for adding points.

local addPoints = {}

local ServerStorage = game:GetService("ServerStorage")

local leaderboardSetup = require(ServerStorage:WaitForChild("LeaderboardSetup"))

leaderboardSetup.leaderboardSetup()

local player = game:GetService("Players")

local leaderstats = player.leaderstats

local points = leaderstats and leaderstats:FindFirstChild("Points")

function addPoints.addPoints()

points.Value = points.Value + 1

end

return addPoints

This is the one that adds the points each hour.

local ServerStorage = game:GetService("ServerStorage")
local addPoints = require(ServerStorage:WaitForChild("AddPoints"))

while true do
	print("yes")
	wait(10)
	for currentValue = 1, 5 do
		print("yeeee")
		addPoints.addPoints()
	end
end

It is saying that leaderstats isn’t a valid member of player in the second script. Please help! I am very new to coding, so I have no idea how to fix this!

1 Like

player:WaitForChild("leaderstats")

If this results in an infinite yield warning then you’ve set up something incorrectly.

This is because you’re calling the player service. Do this instead
Module:

local addPoints = {}

local ServerStorage = game:GetService("ServerStorage")

local leaderboardSetup = require(ServerStorage:WaitForChild("LeaderboardSetup"))

leaderboardSetup.leaderboardSetup()





function addPoints.addPoints(player)
local leaderstats = player.leaderstats

local points = leaderstats and leaderstats:FindFirstChild("Points")

points.Value = points.Value + 1

end

return addPoints

Script that adds the points

local ServerStorage = game:GetService("ServerStorage")
local addPoints = require(ServerStorage:WaitForChild("AddPoints"))

while true do
	print("yes")
	wait(10)
	for currentValue = 1, 5 do
for i, Plr in pairs(game.Players:GetPlayers()) do
		print("yeeee")
		addPoints.addPoints(Plr)
end
	end
end

@62penguins

This is your problem. player needs to be a player object, you have defined it as the players service.

I did this, and the errors I’m getting now are:

  1. Attempt to connect failed: Passed value is not a function
  2. attempt to call a table value
  3. leaderstats is not a valid member of Player

I think the first one has to do with the leaderboard setup script, but I don’t know about the other two.

As someone who has worked on multiple modular code, this is just not the way to go. This is an issue with the leaderboard setup script, as when you call the leaderstats to be added, then .PlayerAdded event will start, you also specify the table module instead of the function. It’ll be better to just start from scratch,
Make this a normal script(one use things like this really dont need to be module), the new leaderboard ssetup


local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)

you can also make the points adding module to this

local addPoints = {}

local ServerStorage = game:GetService("ServerStorage")





function addPoints.addPoints(player)
local leaderstats = player.leaderstats

local points = leaderstats and leaderstats:FindFirstChild("Points")

points.Value = points.Value + 1

end

return addPoints

Thank you so much! This worked out well, and it if finally working!

no problem! glad i could help!

Also, could you briefly explain what this does?

for i, Plr in pairs(game.Players:GetPlayers()) do
			addPoints.addPoints(Plr)
		end

I’m trying to learn from this for future experiences.

basically, Game.players:GetPlayers() will return(or give) a table of all the players currently in the game. The for i, plr in pairs is to loop through each player we got from the :getPlayers() function. It’ll then add the points to the plr.

Thank you! This really helped me understand some more.

1 Like