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.
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.
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
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.
-
Use a variable named
points
to create a new IntValue object usingInstance.new
. -
Set the Name to
“Points”
. -
Set the Value to 0 ; this is what the leaderboard will initially display for the player.
-
Parent the
points
object to theleaderstats
folder. -
local Players = game:GetService(“Players”)
- local function onPlayerAdded(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(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.
-
At the end of the script, create a while loop with
true
as the condition. -
In the loop, wait for 1 second.
-
Players.PlayerAdded:Connect(onPlayerAdded)
- while true do
- wait(1)
- 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 #.
-
Store the result of
Players:GetPlayers()
in aplayerList
variable. -
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. -
while true do
-
wait(1)
-
local playerList = Players:GetPlayers()
-
for currentPlayer = 1, #playerList do
- end
- 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.
-
Store the player at
playerList[currentPlayer]
in a variable calledplayer
. -
Store the player’s Points object in a variable called
points
. -
Set the
Value
property ofpoints
topoints.Value + 1
. -
while true do
-
wait(1)
-
local playerList = Players:GetPlayers()
-
for currentPlayer = 1, #playerList do
-
local player = playerList[currentPlayer]
-
local points = player.leaderstats.Points
-
points.Value = points.Value + 1
-
end
-
end
Test your game and you should find that the leaderboard shows your player’s score counting up by 1 every second.
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 .