#I never came to understand how pairs work.

1 messages · Page 1 of 1 (latest)

pure wyvern
#

what does for i, v in pairs() do mean?

and before you tell me to search through yt vids I did and I just never came to understand it because the videos never answered my questions so I was wondering if someone here could help me get deep into my questions here and what this means and the difference between this and for i = 1 pairs like wtf does that mean?

molten rain
#

i is the key for every element in the table

#

v is the element

pure wyvern
#

so if I was to do

Mypair = {10, 20}

for i, v in pairs(MyPair) do
print(i) it will print 1 and 2?

molten rain
#

{"H" = 5}
i = "H", v = 5

#

yes

#

i is 1, 2 in that case

pure wyvern
#

its kinda hard for me to explain what I am stuck on

#

Sorry if it feels like i am wasting your time

#

But I am telling you like im stuck

#

like when I look at a vid ik what everything means but when I try and do something on my own im stuck.

#

like wtf

molten rain
#

looping through pairs mean you go through each element in a table with i being the key and v being the value of whatever element

pure wyvern
#

mind if I send you some code I am seeing in a yt vid and ask questions on the parts im stuck on?

molten rain
#

if no key is provided then it counts up from 1

#

yes

molten rain
#

go ahead

pure wyvern
#

kk

neat meteor
molten rain
#

^^^^

pure wyvern
#

local MainGame = game.Workspace:WaitForChild("mainGame")
local Gates = MainGame:WaitForChild("Gates")
local Buttons = MainGame:WaitForChild("Buttons")

local function activateButton(v, touched) --why do we have v,touched here? Why can't we leave it blank?
touched.Value = true

local gates = Gates:FindFirstChild(v.Name)

local duration = v:FindFirstChild("Duration")

task.wait(duration.Value)

touched.Value = false

end

for _, v in pairs(Buttons:GetChildren()) do
v.Touched:Connect(function(otherPart) --how does it know what v is? like v is the actual parts in the pair right? Not the number order they are in right?
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
local touched = v:FindFirstChild("Touched")
if humanoid and touched and touched.Value == false then
activateButton(v, touched)
end
end)
end

#

see the commnents I made, those are my questions

pure wyvern
neat meteor
# neat meteor Pairs is used to iterate through key values, including keys that are not numbers...

Then, you have ipairs which can only iterate through arrays with consecutive numbers (1, 2, 3, etc)
-# ⚠️ important to keep in mind that with ipairs, it will stop at the first nil that it finds ⚠️

so for example:

local myTable = {
  "string",
  "woosh",
  nil,
  "another string",
}

for i, v in pairs(myTable) do
  print(i, v)
end

--[[
output would be:
 - 1 string
 - 2 woosh

-- and then it doesn't print more because it reached a nil

]]
molten rain
neat meteor
pure wyvern
#

Sorry english was not my first language

molten rain
#

i isnt a good name for the variable if it isnt numbers only

#

k, v should be used instead

pure wyvern
#

and im still stupid with my other language too

molten rain
#

i, v if you using ipairs()

#

technically lua has no arrays

neat meteor
molten rain
#

only tables

sleek foxBOT
#

studio** You are now Level 2! **studio

pure wyvern
#

i isn't a good name for the variable means i isn't a good way to describe it?

molten rain
#

yes

#

k, v for key, value

pure wyvern
#

yes

#

kk

neat meteor
pure wyvern
#

like 1
2
3

molten rain
#

why dont you test it in roblox studios

neat meteor
neat meteor
neat meteor
pure wyvern
#

so why not table? why array?

molten rain
#

its a table

#

tables can act as arrays

#

arrays cant act as tables

#

arrays are list of items

#

with indices counting up

neat meteor
#

because this:

local myTable = {"a", "b", "c"}

and this:

local myTable = {
 a = "b",
 b = "c",
 d = "e",
}

they're not the same thing.

pure wyvern
#

ohh

#

I see

molten rain
#

array = {"a", "b", "c"}
you can index it by numbers
array[2] gets the second element

pure wyvern
#

consecutive means constant?

molten rain
#

search up the definition

neat meteor
molten rain
#

1, 2, 3 is consecutive

pure wyvern
#

kk

#

is 5,4,3 consecutice?

#

consecutive*

neat meteor
#

So:

{10, 20, 30}

has indices 1, 2, 3 -> these are consecutive

molten rain
neat meteor
#

But this:

local t = {}
t[1] = "a"
t[3] = "b"

has keys 1 and 3, but skips 2 -> these are not consecutive

molten rain
#

elements for tables are both the key and value

pure wyvern
sleek foxBOT
#

studio** You are now Level 6! **studio

molten rain
#

yes

pure wyvern
#

abd = i?

#

kk

molten rain
#

and a= "b" is a key value pair

pure wyvern
#

index means the order of them,?

molten rain
#

also known as a element

molten rain
#

a key thats a number

neat meteor
molten rain
#

usually for arrays

pure wyvern
#

a key?

#

like a key used to unlock doors

#

or an objective

molten rain
#

a is key "b" is value

pure wyvern
#

oh

#

so these keys unlock new stuff? ?

#

wdym

molten rain
#

the key is what you use to select the element you want

pure wyvern
molten rain
#

if you have no key you cant find the element

pure wyvern
#

why can't b be the value like ik i can name them but what they actually define

pure wyvern
molten rain
#

the key is the identification of a element

pure wyvern
pure wyvern
#

or element can also be a string?

molten rain
#

not necassarily

#

in lua a element has a key and value

#

and the key and value can be of any type

#

number, string, boolean

pure wyvern
# pure wyvern local MainGame = game.Workspace:WaitForChild("mainGame") local Gates = MainGame:...

for _, v in pairs(Buttons:GetChildren()) do
v.Touched:Connect(function(otherPart) --how does it know what v is? like v is the actual parts in the pair right? Not the number order they are in right?
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
local touched = v:FindFirstChild("Touched")
if humanoid and touched and touched.Value == false then
activateButton(v, touched)
end
end)
end
what would the key be here then?

molten rain
#

wrap your code with
```lua

```

pure wyvern
#

?

#

wrap my code with lua?

molten rain
#
for _, v in pairs(Buttons:GetChildren()) do
    v.Touched:Connect(function(otherPart) --how does it know what v is? like v is the actual parts in the pair right? Not the number order they are in right?
        local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
        local touched = v:FindFirstChild("Touched")
        if humanoid and touched and touched.Value == false then
            activateButton(v, touched)
        end
    end)
end
#

_ is the key

#

usually _ is used a sort of trashcan

#

meaning the variable wont be used in this code

sleek foxBOT
#

studio** You are now Level 3! **studio

molten rain
#

_ is the key

#

and the code doesnt use the key

near dock
molten rain
#

yea

pure wyvern
molten rain
#

yes

#

its the index in this case

pure wyvern
#

why does it matter? for organization?

molten rain
#

you can

neat meteor
molten rain
#

_ is just saying it wont be used

pure wyvern
#

is it for organization?

pure wyvern
#

or organizing

neat meteor
#

i and v are variables that roblox automatically assigns, and you can name them as you prefer

molten rain
#

its just a stardard to use i, v or k, v for the key/index and value

#

its also a standard to set values your not going to use to _

pure wyvern
#

kk

#

How come I understand everything I see in youtube video tutorials like what the youtubers put in their code I understand it all but when I try and create something myself using what I know and what they showed me my code doesn't work or my mind is blank?

#

Is that normal

near dock
#

That's very normal for beginners, especially if you don't have prior coding experience. It just takes practice.

molten rain
#

i have no idea how to awnser that questiong but that is a normal thing to happen

neat meteor
#

It's harder to think how to make the code yourself than understand others code, it is very normal for beginners

pure wyvern
pure wyvern
#

and everytime I check the tutorial im like "ohh why didn't I think of that?"

molten rain
#

usually when this happens it eighter a syntax issue or a logic issue

neat meteor
# pure wyvern oh alr

It doesn't mean you shouldn't try tho, keep trying until you start getting the hang of it, it's a matter of practice

pure wyvern
molten rain
#

theres a difference between knowing how to code something and knowing why that code works

molten rain
#

you may now know how to use tables

#

but you dont know why they work

#

(hashmaps)

pure wyvern
#

can you give me a very simple learning objective?

molten rain
#

🤔

pure wyvern
#

like something easy with pairs or tables and I try and do it alone and I try and explain whate everything means and does

molten rain
#

make a function that takes the sum of whatever

#

so sum({1,2,3,4,5) returns the value 15 as the awnser

#

because 1+2+3+4+5 = 15

pure wyvern
#

using pairs and tables?

molten rain
#

the input is a table

#

the output is the loop

neat meteor
pure wyvern
neat meteor
#

There

molten rain
#

have you coded in any other programming languages

molten rain
neat meteor
#

no, i havent

molten rain
#

oh alright

pure wyvern
#

for index = 1, #testArray do
print(index, testArray[index])
end

does for i = 1, #testarray mean it goes from 1 to how many number of index there is in the array?

#

or number of elements

#

@molten rain @neat meteor sorry for the ping I forgot to reply to one of you

molten rain
#

yes

#

1,2,3...#testArray

neat meteor
pure wyvern
#

kk

pure wyvern
neat meteor
#

uhhh

pure wyvern
#

within what we have been leaening

neat meteor
#

idk

pure wyvern
#

its ok if u don't want to

#

but

#

it would help

neat meteor
#

make a for loop that iterates through a table, searching for a specific string or smth idk

pure wyvern
#

kk

pure wyvern
#

instead of just plain copy and paste?

pure wyvern
#

**js local MyTable = {10, 20, 30, 40, 50}

for i, v in pairs(MyTable) do
print(i)
if i == 3 then
print("its the third one")
end
end**

#

oh

neat meteor
#

its this key: `

pure wyvern
#

`

#

lua

#

kk

#

ty

#

for i, v in pairs(MyTable) do
    print(i)
    if i == 3 then
        print("its the third one")
    end
end````
neat meteor
#

lua goes next to the 3 first `

pure wyvern
#

oh

#

for i, v in pairs(MyTable) do
    print(i)
    if i == 3 then
        print("its the third one")
    end
end```
#

this works

#

17:49:31.235 1 - Server - Script:4
17:49:31.235 2 - Server - Script:4
17:49:31.235 3 - Server - Script:4
17:49:31.235 its the third one - Server - Script:6
17:49:31.235 4 - Server - Script:4
17:49:31.236 5 - Server - Script:4

#

went into the output

#

did I do what you said?

neat meteor
pure wyvern
#

When it noticed it was the third one I made it print its the third

pure wyvern
#

wdyj

#

wdym

#

oh

neat meteor
#

right now, you search for the specific index

pure wyvern
#

ye

neat meteor
#

but i wanted you to search for the value in the table

pure wyvern
#

ohh

#

for i, v in pairs(MyTable) do
    print(v)
    if v == 30 then
        print("its the third one")
    end
end```
#

does this work?

#

i mean it does

#

but

neat meteor
#

i want you to search for the third item in the table

pure wyvern
neat meteor
#

you can access tables contents like this:

local myTable = {10, 20, 30, 40}

print(myTable[3])
-- output: 30
pure wyvern
#

kk

pure wyvern
#

17:57:00.954 10 - Server - Script:4
17:57:00.954 20 - Server - Script:4
17:57:00.954 30 - Server - Script:4
17:57:00.954 its 30 now - Server - Script:6
17:57:00.955 40 - Server - Script:4
17:57:00.955 50 - Server - Script:4

#

or i can do'

#

for i, v in pairs(MyTable) do
    print(v)
    if v == MyTable[2] then
        print("its 20 now")
    end
end```
neat meteor
#

yh

pure wyvern
#

W

#

ty

pure wyvern
neat meteor
#

i'd prefer not to

#

sorry

pure wyvern
#

thank you