Hello!
Recently I’ve seen a request on another discord server where someone asked for a leaderboard script. The one that will list all the players in the game and display their stats, like kills/deaths and the like.
To be fair, I’ve never made this before, so I set myself a goal for today and tried to come up with my own solution, without watching tutorials.
The project file is below and the code as well, just incase if you don’t feel like opening the file yourself.
It’s not supposed to look fancy, it’s all about the functionality.
Think this could all be done better? Let me know!
– Happy coding everyone
Hierarchy
ShowLeaderboard Script
-- ==============
-- SERVICES
-- ==============
local UIS_SERVICE = game:GetService("UserInputService")
local REP_STORAGE = game:GetService("ReplicatedStorage")
local PLR_SERVICE = game:GetService("Players")
local GUI_SERVICE = game:GetService("StarterGui")
-- Hide default Leaderboard
GUI_SERVICE:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
-- ==============
-- FUNCTIONS
-- ==============
local function clearLeaderboard()
-- Loop through each child in the scrollFrame
for i, child in pairs(script.Parent.PlayerList:GetChildren()) do
-- Make sure it's a frame, not anything else
if child:IsA("Frame") then
-- If it is, destroy it >:)
child:Destroy()
end
end
end
local function getPlayers()
-- Loop through all the players in the game
for i, plr in pairs(PLR_SERVICE:GetPlayers()) do
-- Check if they don't already exist inside the leaderboard (which they shouldn't but just incase)
if not script.Parent.PlayerList:FindFirstChild(plr.Name) then
-- Grab their kills and deaths values
local kills = plr.leaderstats:WaitForChild("Kills").Value
local deaths = plr.leaderstats:WaitForChild("Deaths").Value
-- Clone the playerFrame from replicated storage and set it's values
local playerFrame = REP_STORAGE:FindFirstChild("Player"):Clone()
playerFrame.Name = plr.Name
playerFrame.PlayerName.Text = plr.Name
playerFrame.KillDeaths.Kills.Text = kills
playerFrame.KillDeaths.Deaths.Text = deaths
playerFrame.LayoutOrder = i
-- Put it inside the leaderboard
playerFrame.Parent = script.Parent.PlayerList
end
end
end
local function OnInputBegan(input)
-- Check if the user is using a keyboard
if input.UserInputType == Enum.UserInputType.Keyboard then
-- Check if they pressed Tab
if input.KeyCode == Enum.KeyCode.Tab then
-- Make the leaderboard visible
script.Parent.Visible = true
-- Populate the leaderboard
getPlayers()
end
end
end
local function OnInputEnded(input)
-- Check if user is using a keyboard
if input.UserInputType == Enum.UserInputType.Keyboard then
-- Check if they pressed Tab
if input.KeyCode == Enum.KeyCode.Tab then
-- Clear the leaderboard from those stinky frames
clearLeaderboard()
-- Set it to not visible
script.Parent.Visible = false
end
end
end
-- ==============
-- CONNECTIONS
-- ==============
UIS_SERVICE.InputBegan:Connect(OnInputBegan)
UIS_SERVICE.InputEnded:Connect(OnInputEnded)
Project File: leaderboard_v1.rbxl (37.3 KB)