Can you explain these codes?

Hey i am trying to learn Roblox scripting .And i have some problem.Can you explain this for me?
I can’t understand it.This code i got from here:Scoring Points | Roblox Creator Documentation
Here is it, the code which is the problem make me feel tired because i don’t understand it.

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)
1 Like

It makes a basic leaderstats system, like coins

Remember the coins in certain games at the leaderstats?

1 Like

No,i want to know that why exist this code i try to understand it .

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

That makes the leaderstats of the player, that’s basically the core of the leaderstats.

2 Likes

Leaderstats is a variable you make and give name right?

No, it’s a folder and it must be named leaderstats to create the leaderstats

Wait, it is a folder before i use insance.new(“Folder”)?

From the past they made it values instead of folder, now it’s folders because it’s the correct way and more efficient

But for coins they make it values, but leaderstats are still folders

Also, no

local Players = game:GetService("Players") -- Getting players service

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder") -- creating folder
	leaderstats.Name = "leaderstats" -- naming it 'leaderstats'
	leaderstats.Parent = player -- putting folder to player

	local points = Instance.new("IntValue") -- creating int value
	points.Name = "Points" -- naming it 'Points'
	points.Value = 0 -- setting value of Points
	points.Parent = leaderstats -- putting it to leaderstats
end

Players.PlayerAdded:Connect(onPlayerAdded) -- function 'onPlayerAdded' will fire, when player joined to game
2 Likes

So why we must use insance.new(“Folder”).What is the usage?

To create the leaderstats, it’s simple . Because if we use values as leaderstats it would be the wrong way

You have to use that to create a folder that will hold all your stats in, that line of code there makes a folder appear when they join. So they always have leaderstats, since you can’t drag and drop the folder into the player you use that line of code to basically do it for you.

The leaderstats is the name we can’t change to make it know that leaderstat we are coding is the right leaderboard right?

Why must leaderstats, we can’t change the name like a variable in C right?

Correct, the folders name MUST be named “leaderstats” with no caps or mispelling or it won’t correctly run. The values inside can be changed to whatever you want though!

Oh,i understand so it the reason why we have the code that is leaderstats.Name = “leaderstats” to change the name we like right?

Yes, but we can rename stuff like coins

local points = Instance.new("IntValue") 
points.Name = "Points" -- you can change coins to whatever u want
1 Like

Correct again! If we didn’t set the name to leaderstats then the name would stay as folder, which would make the whole stats not work.

1 Like

Not break but the stats wouldn’t work

1 Like

It has to be called leaderstats so the menu shows up.
image

Any other folder that isn’t called “leaderstats” won’t make this show.
You can only name the values inside like in the image I showed they named the IntValue’s name to Gold.

2 Likes