I seen SOME people struggle to make a player leaderboard, so I decided to make this tutorial. Hope this is helpful for you
SPOILER :
Saving data is the most hardest, breathtaking, insanely hard task to do (no itâs not).
Three things before you give me feedback
- I am not some expert scripter, probably not me suddenly lost interest in making game
- This is my first tutorial so be easy on me.
- The knowledge that I had gathered from my coding experience finally being used to make this. Actually, if you want the full code you just need to scroll down and BOOM, there is it
Ahem, back to the topic. As the title suggest, this tutorial will show you on how to make a player leaderboard or in the other word âIn-Experience Leaderboardâ .
Donât ask why I said âIn-Experience Leaderboardâ
The FIIIIIRRRRRRRSSSTTTTT thing is YOU need to do is to make a script, parent it to ServerScriptService, and finally name it âPlayerLeaderboardManagerâ (or anything you want it to be named) as the example shown below :
Open the âscriptâ and write the code shown below :
-- \\ I'm pretty sure you know the basic already. \\
-- Get all the nessecary Services that will be needed in this script
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
-- Get a DataStore named "PlayerData" or whatever the DataStore name is
local PlayerData = DataStoreService:GetDataStore("PlayerData")
-- Quite obvious isn't it?
local IntValuesNames = {
"Wins",
"Points"
}
Players.PlayerAdded:Connect(function(Player)
-- Make a folder named "leaderstats" and parent it inside of the Player
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats" -- MAKE SURE THE "L" IS LOWERCASE OR ELSE THE LEADERSTATS WON'T SHOW
end)
Explanation : When the player joined the game it will make a folder that will be named âleaderstatsâ and will be parented to the player.
The next step is to make the IntValues and naming them, which is simple ( Put this after the leaderstats one ).
-- We will do make the IntValues have some "value" later since we don't show the "Save Data Management"
for Index, Name in pairs(IntValuesNames) do
-- Create a IntValue and parent it to leaderstats. And this time finally naming it.
local IntValue = Instance.new("IntValue", Leaderstats)
IntValue.Name = Name
end
Explanation : So for every stuffs that stored inside of âIntValuesNameâ will make a IntValue and will be named after the stuffsâ name
Now lets make the âSaving Systemâ and âLoad Data Systemâ
First we are going to make the saving system since you probably wouldnât understand why I do this instead of making the data and setting the IntValue âvalueâ to that data
for Index, Name in pairs(IntValuesNames) do
-- Create a IntValue and parent it to leaderstats. And this time finally naming it.
local IntValue = Instance.new("IntValue", Leaderstats)
IntValue.Name = Name
end
Okay, now letâs get on the code
-- On the player removal, it will save the IntValues "value"
Players.PlayerRemoving:Connect(function(Player)
-- Get the player's leaderstats
local Leaderstats = Player:FindFirstChild("leaderstats")
-- Table because table gud
local SavedStuffs = {}
-- For every IntValues in the leaderstats will be inserted to the table, "SavedStuffs"
for _, IntValue in pairs(Leaderstats:GetChildren()) do
-- Insert the value to the "SavedStuffs"
table.insert(SavedStuffs, IntValue.Value)
end
-- Make a pcall function about setting the "PlayerData" async
local success, errorMessage = pcall(function()
PlayerData:SetAsync("PlayerData_"..Player.UserId, SavedStuffs)
end)
-- Check if the success isn't true. If then, print the "errorMessage"
if success ~= true then
print(errorMessage)
else
print("Successfully saved data!")
end
end)
Explanation : When the player left the game, it will get the playerâs âleaderstatsâ children value and store them on a table, âSavedStuffsâ. After that it will make a pcall function about setting the PlayerData async and check if the âsuccessâ is really a success. If not then, it will print the âerrorMessageâ
After doing that, letâs go to the âLoad Data Systemâ one
Players.PlayerAdded:Connect(function(Player)
-- First of all, make a variable that will be called "data"
local data
-- Make a pcall function about getting the "PlayerData" async
local success, errorMessage = pcall(function()
data = PlayerData:GetAsync("PlayerData_"..Player.UserId)
end)
-- Check if the success isn't true. If then, it will print the "errorMessage"
if success ~= true then
warn(errorMessage)
else
print("Successfully loaded data!")
end
-- Make a folder named "leaderstats" and parent it inside of the Player
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats" -- MAKE SURE THE "L" IS LOWERCASE OR ELSE THE LEADERSTATS WON'T SHOW
for Index, Name in pairs(IntValuesNames) do
-- Create a IntValue and parent it to leaderstats. And this time finally naming it.
local IntValue = Instance.new("IntValue", Leaderstats)
IntValue.Name = Name
-- Check the data isn't nil
if data ~= nil then
-- Set the IntValue "value" to the set data
IntValue.Value = data[Index]
else
IntValue.Value = 0
end
end
end)
There will be no explanation for this one.
Please do note that I am not some expert scripter, and also this is my first tutorial so it may not be great.
Summary
Some random guy decided to make a leaderstats tutorial because he seen some people struggle to make a simple leaderstats.
- This is a VERY GOOD tutorial, it helped me out on making a leaderstats
- Good tutorial bro
- Nice tutorial, even though it has some flaws
- Bad, because itâs bad
- Itâs the worst tutorial I ever seen in my entire lifetime.
0 voters
Those who are lazy, here is the full code :
-- \\ I'm pretty sure you know the basic already. \\
-- Get all the nessecary Services that will be needed in this script
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
-- Get a DataStore named "PlayerData" or whatever the DataStore name is
local PlayerData = DataStoreService:GetDataStore("PlayerData")
-- Quite obvious isn't it?
local IntValuesNames = {
"Wins",
"Points"
}
Players.PlayerAdded:Connect(function(Player)
-- First of all, make a variable that will be called "data"
local data
-- Make a pcall function about getting the "PlayerData" async
local success, errorMessage = pcall(function()
data = PlayerData:GetAsync("PlayerData_"..Player.UserId)
end)
-- Check if the success isn't true. If then, it will print the "errorMessage"
if success ~= true then
warn(errorMessage)
else
print("Successfully loaded data!")
end
-- Make a folder named "leaderstats" and parent it inside of the Player
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats" -- MAKE SURE THE "L" IS LOWERCASE OR ELSE THE LEADERSTATS WON'T SHOW
for Index, Name in pairs(IntValuesNames) do
-- Create a IntValue and parent it to leaderstats. And this time finally naming it.
local IntValue = Instance.new("IntValue", Leaderstats)
IntValue.Name = Name
-- Check if the data isn't nil
if data ~= nil then
-- Set the IntValue "value" to the set data
IntValue.Value = data[Index]
else
IntValue.Value = 0
end
end
end)
-- On the player removal, it will save the IntValues "value"
Players.PlayerRemoving:Connect(function(Player)
-- Get the player's leaderstats
local Leaderstats = Player:FindFirstChild("leaderstats")
-- Table because table gud
local SavedStuffs = {}
-- For every IntValues in there will be inserted to the table, "SavedStuffs"
for _, IntValue in pairs(Leaderstats:GetChildren()) do
-- Insert the value to the "SavedStuffs"
table.insert(SavedStuffs, IntValue.Value)
end
-- Make a pcall function about setting the "PlayerData" async
local success, errorMessage = pcall(function()
PlayerData:SetAsync("PlayerData_"..Player.UserId, SavedStuffs)
end)
-- Check if the success isn't true. If then, print the "errorMessage"
if success ~= true then
print(errorMessage)
else
print("Successfully saved data!")
end
end)
Ah yes, spoiler. Who spread this spoiler shall be uh, I donât know
Edit 1 : English and code mistakes.
Edit 2 : @xGOA7x point out that player can change their username and will loss their data. My brain suck