Why use this in my script

ok so in this script, there I added leaderboard and then I followed a roblox tutorial on the roblox wiki and it gives the players a point every second, the code is here below… But I don’t understand the local player = playerlist[currentPlayer], I was wondering why did they use the “[]” brackets? Can anybody tell me why?

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)

– giving players points

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

Here they used [] to indicate they want to get a specific player from playerList. The function of [] in a table is to retrieve something with a specific index. Say we have a table

local fruits = {"Apples","Pears","Bananas"}

And we want Pears, we can get it via

fruits[2]

Which gets the 2nd thing in the table, which is “Pears”. It works the same for your example. playerList is a list of all the players in the game currently, and we know this by:

local playerList = players:GetPlayers()

A for loop is a numerical loop, and we always have to make a new variable to hold the current iteration. What

playerList[currentPlayer]

Does is get the player in the index using the current iteration of the loop

So if they’re ordered as

{"EmbatTheHybrid", "JackyBoi", "BananaLover"}

When the loop is on it’s 2nd iteration, player will be the 2nd thing i nthe thing, JackyBoi

2 Likes

that is finding the value of the specific index of the table.

1 Like

The [] is almost the same as “.”
points.Value is the same as points["Value"]
The [] allows you to have a variable inside that

1 Like

@EmbatTheHybrid explains the [] very well, but what you may have missed, if you already knew what tables were, is that this line right here creates a table:

local playerList = players:GetPlayers()

So at that point, playerList is a table which you can then index with []

I think the naming of that variable currentPlayer could be confusing you as well, its just a number in the index that is being cycled through. I might be more appropriately named ‘thisplayer’ or ‘count’

1 Like

However, in this specific case, the example uses a dictionary, which instead of using a list, it sets values to a certain key. Here is a good analogy:

A lua table is similar to a real-life table, as it holds things in order.
A lua dictionary is similar to a bookshelf, where you can get data based on a certain key. In a bookshelf, you index books using an authors name.

1 Like

What example are you referring to, the one OP posted? Where do you see a dictionary if I may ask?

GetPlayers() returns a table of all the players currently in the server, you can get a specific value in tables via their index number aswell

eaxmple

local fruits = {"Apples","Bananas","Pineapple"}

print(fruits[1]) -- prints Apples since it's the first thing in the table
1 Like

Oh mb, I am really tired right now lol. Yeah it is a table, but it is still good to know what a dictionary is.

2 Likes

Haha it’s okay, being tired gets the better of us sometimes, but yea, dictionaries are also good to know for development as well, but I don’t think it’s needed in this case since we’re focusing on tables

2 Likes

Ur right tho it isn’t that hard.

1 Like

Here they used [] to indicate they want to get a specific player from playerList. The function of [] in a table is to retrieve something with a specific index. Say we have a table

shortly said…

1 Like

ok this is helping A LOT thank you :), But I am still a little confused, now that you mentioned current player, I didn’t see a variable or anything about it, does the script just know it sometimes or is there something to it, I am referring to this part.

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

currentPlayer is defined as the for loop’s control variable.

while true do
	wait(1)
	local playerList = players:GetPlayers()

	for currentPlayer = 1, #playerList do -- defined here!
		local player = playerList[currentPlayer]
		local points = player.leaderstats.Points
		points.Value = points.Value + 1
	end
end

Read more about for loops here: Introduction to Scripting | Roblox Creator Documentation


Using for loops to iterate over arrays, in my opinion, is terrible. It harms code readability and causes a lot of confusion. Some people claim that it improves performance, but thats only by nanoseconds, it seriously won’t affect your game at all.

When iterating over arrays, you should instead use pairs or ipairs.

while true do
	local playerList = players:GetPlayers() -- you can skip defining this variable, and just replace playerList with this if you want
	for _, player in pairs(playerList) do -- this is the line i'm talking about below
		local points = player.leaderstats.Points
		points.Value += 1 -- you can only do this in Luau (not vanilla Lua), it means the samething as "points.Value = points.Value + 1", but is shorter and nicer to read
	end
	
	wait(1)
end

The _, player is defining the variables that the loop will use. The first variable (_) is assigned the index, the second (player) is assigned the value. _ is used because it’s a “dummy variable”, meaning that I won’t be using it in the code (this doesn’t impact performance afaik, its just for readability).

1 Like

WOW! This is REALLY GOOD :smiley: that was an outstanding explanation! thank you :smiley:

Cool, glad you’re getting it now. I explained how the variable name was confusing in post #5 of this thread.

1 Like

And thank you everybody else to :smiley: THESE WERE AMAZING!

1 Like