If you need to go through a table, a table is a collection of values, objectvalues, numbervalues, boolvalues etc.
If for example you need to update the color of all parts in a folder, you can do
for i, part in pairs(Folder:GetChildren()) do --The :GetChildren() returns a table of all of the folder's contents.
part.BrickColor = --whatever color
end
the “i” value is the number of iterations that has been currently done. so after going through 3 parts, the i will be 3. the “part” is the value that it’s currently at.
You would need to look into for loops to use in pairs. Most scripts using in pairs look something like this:
for i,v in pairs (game.workspace:GetChildren()) do -- Filters though every brick in workspace
if v:IsA('Part') then -- Checking each part in workspace to see if it is a part
v.BrickColor = BrickColor.New('Really Red') -- altering the specific part
end
end
These are incredibly useful for things like vehicle hitboxes or for applying effects to multiple bricks in your game by iterating the array that is returned by the in for loop. You can read up on more about them on the API here:
They are also used a lot when creating things like admin scripts to filter though settings to see who is an admin etc and for games that might have whitelisted players who if their name is in a script they get a special tool etc
for i, v in pairs(game.Players:GetPlayers()) do -- Looping through the players
if v:FindFirstChild("IsKiller") then -- Checking to see if they have a value in them
v.Character.HumanoidRootPart.CFrame = game.workspace.KillerSpawn.CFrame + Vector3.new(0,10,0) -- Changing the killers postition
game.ReplicatedStorage.Int.Value = "Starting game..." -- Setting the game to start
end
end
This is part of a basic loop for a round based game.
I know it returns an array, i am simplifying it for him because i assume he doesnt know what tables / arrays are.
Figuritavely speaking though, they’re both the same if you think about it:
Item:GetChildren() = {Item.Part1, Item.Part2 etc…} So that’s why i reffer to both of them as tables
also @xNick_O he puts the [x] after table, (x being a number) to make refference to the xth item in that table
That’s only when iterating over numerical indices. pairs just returns the next iterator function, the index- be it numerical or a string along with the associated value, whatever variables you pass i.e i, v they contain the values returned.
Technically still a table though, the term ‘array’ is just by Roblox standards, a table with numerical indices.
pairs is very often used in the wrong situations, always use ipairs to iterate over contiguous arrays, unless you want to iterate in arbitrary order with negligible increased speed.
The example I used doesn’t matter if you use in pairs or ipairs
It won’t affect the outcome, but examples like these are what sprout incorrect usage of pairs, if you can iterate a tiny bit faster then why not??
You can also use in pairs to use a function for multiple parts. For example, if you wanted multiple parts to kill a player, you could put all of them in a folder named “Kill Bricks”. You would then use “in pairs” to apply a kill function for all those parts.
local killparts = workspace:FindFirstChild("Kill Bricks")
for _, v in pairs(killparts:GetDescendants()) do --gets all descendants of the folder
if v:IsA("BasePart") then --checks if the current object is a part (it could possibly be a model, and we don't want that)
v.Touched:Connect(function(hit) --If any part inside the folder is touched
local character = hit:FindFirstAncestorOfClass("Model")
if character and character.Humanoid then
character.Humanoid.Health = 0
end
end)
end
end
Then they’re missing out on micro-optimization, unless they’re iterating over non-list items (a dictionary for example) in which case pairs is just supposed to be used.
From another post of mine:
Again, it’s clearly just clearly negligible difference , there shouldn’t be too much of a difference unless you’re using pairs for traversing a huge array and using the counter index.
A table and an array aren’t different datatypes, so
print(type({['Key'] = "value"}))
-- and
print(type({"value"}))
-- will both print 'table'
-- to check for a typical table, do
local tab = {}
if type(tab) == "table" and #tab ~= 0 then
print("array")
else
print("dictionary")
end
@Abysmallow all posts made before this explain its usage, that should be enough for concept. Just use it as an iterator to reduce the amount of code, when you need to perform multiple repetitive operations for multiple objects, for example.
I found this post a bit misleading, so I just want to correct a few misconceptions:
An array is just a type of table with numeric incremental (aka 1, 2, 3) indexes. I’m not sure this is a nitpick worth making, since it is still a table.
You really should not be using in pairs for looping through an instance’s children. ipairs is generally preferred for arrays, and GetChildren will always return an array. ipairs is preferred partially because it’s faster, but also because it makes it clear what type of indexes you’re going to be expecting within the loop.
That function will only return 1 and the first element of Table; return immediately stops a function. It also completely ignores the fact pairs will loop through non-numeric indexes, like in {["hey"] = "yo"}
GetDescendants also returns an array, so ipairs should also be used here. I’m also not sure how helpful "i" is the "i" is, since that wouldn’t have helped me at all as a beginner.
Indexes can be any type. You can even index a table with a table! For example:
local t = {}
t[t] = t -- the value of the table at the position which is itself would be itself
print(t[t][t] == t) --> true
(I’m sure this is just a typo)
I mean, technically, it prints d. The quotation marks just signify that it’s a string in the code, they don’t appear in the output
I found this a bit misleading, so this is just to clarify: for i = 1, #Table do will just make a loop run once for each item of the table. i is simply a number that goes up each time. Since Tableis an array in this case, any number between 1 and its length is a valid index, since that’s the definition of an array. It’s not really looping through the table like ipairs or pairs are, though, it’s just counting up from 1 to the table’s length in a way that happens to be useful for indexxing an array. This is pretty nitpicky and doesn’t really make a difference, but I think the distinction is important because it helps show how for i = x, y, z can be useful in other scenarios too.
That only works for arrays, not all tables. It won’t count non-numeric indexes. For example, a table like {["hey"] = "yo"} would have a length of 0.