First off, we will need to add a script to ServerScriptService. Feel free to rename it whatever you would like.
Now, put the following code in the script.
It will create a folder in the player called
game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Parent = player leaderstats.Name = "leaderstats" end)
Now, run the game. Look at the top-right. If you typed the script correctly, you should not see anything. However, if you look in your Explorer and open Players, then open your player object, you should see a folder called leaderstats. Now, in order to actually add something to our leaderboard, change your code to the following example.
game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Parent = player leaderstats.Name = "leaderstats" local coins = Instance.new("IntValue") -- Can be IntValue, BoolValue, StringValue, etc... coins.Parent = leaderstats coins.Name = "Coins" end)
This script will create an integer (a whole number) value inside the Leaderstats folder and name it "Coins." Now, if we run, we will see that above the player list, there is a value that says "Coins" Coins should be 0, as that is the default value for IntValues.
#Saving Players' Leaderstats Saving players' leaderstats is simple, in fact, I've already written a script that will save and load all of them! Make sure this is its own script, we don't want copy & paste issues. Make sure this is a Script, not LocalScript or ModuleScript!
local DataStoreService = game:GetService("DataStoreService") -- We need DataStoreService to save data. I'll make a tutorial on it soon. local DataStore = DataStoreService:GetDataStore("MyDataStore") -- Change MyDataStore to whatever you want. -- SAVING -- game.Players.PlayerRemoving:Connect(function(player) -- This is called when a player is leaving. local plr_data_key = tostring(player.UserId).."_data" -- The main key for storing out data. This is important, don't change it! for count, object in pairs(player:WaitForChild("leaderstats"):GetChildren()) do DataStore:SetAsync(plr_data_key.."-"..object.Name, object.Value) -- With my UserId and the coins leaderstat, this would be saving to "1217140517_data-Coins" end end) -- LOADING -- game.Players.PlayerAdded:Connect(function(player) -- This is called when a player has joined. local plr_data_key = tostring(player.UserId).."_data" for count, object in pairs(player:WaitForChild("leaderstats", 5) do local value = DataStore:GetAsync(plr_data_key.."-"..object.Name) object.Value = value end end)
Hope I could help!