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
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
@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’
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.
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
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
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
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).