#What are Pairs & IPairs?

1 messages · Page 1 of 1 (latest)

wooden breach
#

Every tutorial I watch about those two, I don't seem to understand it.. I might be slow or something? Idk😅

dense wave
#
local myDictionary = {
    ["Blue Player"] = "Ana",
    ["Gold Player"] = "Binh",
    ["Red Player"] = "Cate",
}

for key, value in pairs(myDictionary) do
    print(key .. " is " .. value)
end```
#

Pairs is used with dictionaries, and just returns the key and value in the dictionary

#

Ipairs, is for table or lists, whatever you call it

#
local players = {"Ali", "Ben", "Cammy"}

for playerRank, playerName in ipairs(players) do
    print("Winner #" .. playerRank .. " is " .. playerName)
end```
#

Loops through the elements in order using ipairs

#

Here are the docs, might expalain them a bit better than the tutorials

#

also nowadays you don't need to use pairs