#Loops in table

1 messages · Page 1 of 1 (latest)

woven mirage
#

Hello, could anyone just tell me whats the difference between :

for key, value in ipairs(table) do 

value = 5 -- the values in the table will not change when i will print the table after

end

And

for key, value in ipairs(table) do 

table[key] = 5 -- the values in the table will change when i will print the table after

end

I dont understand since like table[key] is same as value, so why it dont work as the same way when i change the value for example ?? Hope someone can answer me, thanks

sour mango
sleek tundra
#

with table[key] you are directly changing the table, not trying to change the value of a variable

woven mirage
#

Of each variable in tables ?

#

Or it does nothing

sleek tundra
#

the variable is well, a variable, it has no link to the table and wont be changed (in the table)

doing table[key] directly changes the value in the table

sleek tundra
#

kinda hard to word it

#

to make it simple to understand

woven mirage
sleek tundra
#

my brain cant comprehend that

#

but pretty much ur writing to the variable by changing "value" but writing to the table with table[key]

woven mirage
#

If i

#

For example

#

Wait

#

Let me try

#

@sleek tundra no it doesnt work

#

It doesnt change anything

sour mango
woven mirage
#

Only if i do t[index] ig

woven mirage
woven mirage
#

but so if i do now theTable[key], it dont change the initial variable value but value of the variable IN the table, see :

woven mirage
sour mango
woven mirage
#

but ye u right thx

hexed estuary
# woven mirage Hello, could anyone just tell me whats the difference between : ```lua for key, ...

the difference between the first (value=5) compared to the second (table[key]=5) to assign value into a table? well that boils down to what's being assigned to. local foo=bar. this sets foo to bar. i can then set foo to anything else and it won't do anything to bar. local foo=bar; foo=3; -- nothing happens to bar. on the other hand, tables pass by reference, meaning if i do foo={} bar=foo then foo and bar are the same table instead of something like a literal string or number, meaning if you change something from anywhere that has a reference to that specific table, then the change will appear in both.

#

Instances work this way a lot, like you can do playeradded(plr) plr.Character.Humanoid.Health=0 and it sets the humanoid's health to 0, the humanoid being a table in this case. if you did thing=plr.character.humanoid.health ... later ... thing=0 then you aren't changing the table, you're changing the literal number stored in thing.

#

hope that helps

unborn gust
woven mirage