Can you explain these codes?

What did you mean?Can you give me that code to make me understand more?

I mean where is the code make the system know,where is it?Can you give me?Please

I mean where is the code make the system know,where is it?Can you give me?Please

When a player joins the system checks if the player has a leaderstats folder inside the player.
And if they do it adds them to the leaderboard.

local Players = game:GetService("Players")-- The player service
 
local function onPlayerAdded(player)--Using the player the function got from the player added event.
	local leaderstats = Instance.new("Folder")--Makes a folder 
	leaderstats.Name = "leaderstats"--Names it leaderstats so the system knows you want to make a leaderstat
    leaderstats.Parent = player--Put it inside the player so the system can check if the player will be added to the leaderboard
 
	local points = Instance.new("IntValue")--The value which holds integers
	points.Name = "Points"--Name it whatever you want.
	points.Value = 0--Change how many points the player will have at the start
	points.Parent = leaderstats--Put it inside the leaderstat folder so the system can take it and add the points value next to their name
end
	
Players.PlayerAdded:Connect(onPlayerAdded)--Line executes every time a player joins and does the onPlayerAdded function above. It also gives it the player that just joined.
1 Like

I just have a problem create a new leaderstats need any code have the words"leaderstats" right?After 51 post i realize the system know we are coding about it that because the line


leaderstats.Name = "leaderstats"

Right?

Right but it has to be inside the player so it knows which player has the leaderstat.

leaderstats.Parent = player

Wow ,that’s it ,i need it.That because the line leaderstats. Name

Can you give me the usage of the code .Name why it can do that(make the system know)?

I think it just give the name for it

It’s inside the Roblox engine, you do not code it, the engine detects it automatically. The Roblox engine is not open-sourced (albeit Luau is)

Yes that’s what it checks to know if it’s a leaderstat.
image
1: The player service

local Players = game:GetService("Players")

2: The player (in this script the PlayerAdded event runs the functions and passes the player value)

local function onPlayerAdded(player)

3: The folder which got named “leaderstats” so the system knows it’s a leaderstat and not a random folder.

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

4: The IntValue you put inside of it.

local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
1 Like

Scoring Points

PART 2 - SCRIPTING THE POINTS

Points Value

The leaderboard system reads any values in the leaderstats folder and displays whatever it finds.

To add a stat which will track a player’s points, a new IntValue object can be parented to the leaderstats folder. The name of the value object will be displayed alongside its current value.

  1. Use a variable named points to create a new IntValue object using Instance.new .

  2. Set the Name to “Points” .

  3. Set the Value to 0 ; this is what the leaderboard will initially display for the player.

  4. Parent the points object to the leaderstats folder.

  5. local Players = game:GetService(“Players”)

  • local function onPlayerAdded(player)
  1. local leaderstats = Instance.new(“Folder”)
  2. leaderstats.Name = “leaderstats”
  3. leaderstats.Parent = player
  • local points = Instance.new(“IntValue”)
  1. points.Name = “Points”
  2. points.Value = 0
  3. points.Parent = leaderstats
  4. end
  • Players.PlayerAdded:Connect(onPlayerAdded)

Test your game and you should see the leaderboard appear in the top right with the names of your players and a points score next to them.

Counting Time

Each player should earn a point for each second they are alive. A while loop and the wait function can be used to update the value of points every second.

  1. At the end of the script, create a while loop with true as the condition.

  2. In the loop, wait for 1 second.

  3. Players.PlayerAdded:Connect(onPlayerAdded)

  • while true do
  1. wait(1)
  2. end

Player List

To run code for every player in the game, you’ll need to iterate through the array of players returned by the GetPlayers function.

An array is a list of items stored in order. Each item can be accessed by its index position, starting from 1 . You can get the length of an array by prefixing it with #.

  1. Store the result of Players:GetPlayers() in a playerList variable.

  2. Create a for loop with a starting value of 1 and an end value of #playerList , so that you get one iteration of the loop per player.

  3. while true do

  4. wait(1)

  5. local playerList = Players:GetPlayers()

  6. for currentPlayer = 1, #playerList do

  • end
  1. end

Awarding Points

To award a point to each player in the for loop, you’ll need to get the player out of the array and add 1 to the Points object stored in their leaderstats folder.

Accessing Array Values

Objects stored in an array are accessed using square brackets - for instance, the first item in the playerList array can be accessed with playerList[1] .

If you write playerList[currentPlayer] in the for loop, you can move through each player in the list with every iteration of the loop.

  1. Store the player at playerList[currentPlayer] in a variable called player .

  2. Store the player’s Points object in a variable called points .

  3. Set the Value property of points to points.Value + 1 .

  4. while true do

  5. wait(1)

  6. local playerList = Players:GetPlayers()

  7. for currentPlayer = 1, #playerList do

  8. local player = playerList[currentPlayer]

  9. local points = player.leaderstats.Points

  10. points.Value = points.Value + 1

  11. end

  12. end

Test your game and you should find that the leaderboard shows your player’s score counting up by 1 every second.

2 Likes

Oh wow ,THANK YOU so much for spending time helping me.That’s enough.When look at your picture and listen to your explanation ,i understood, that’s what i want to know.Thank you again .