How does this work

local heightmap = {}
local hx = {}
for x = 1,100 do
     heightmap[x] = hx
end
for z = 1,100 do
     hx[z] = "hi"
end
print(heightmap[2][3]) --- prints "hi"

if that works then why doesnt this work

local dog = “hi”
local cat = dog
print(cat) – prints “hi”
dog = “lol”
print(cat) – still prints “hi”

1 Like

how did my z loop update the values of heightmap i am so confused

String values are passed by value, not reference. So when you say:

local dog = “hi” 
local cat = dog

You create 2 separate string values named dog and cat with the same content and when you change one, other one stays as it is.

yeah thats for strings and i understand that, but what about that first table thing,
does it not act the same way?

Here you are creating 100 references to the same table that hx points to. It’s the exact same table in memory that is being pointed to. But like mentioned above strings are copied.

1 Like

Tables are passed by reference, which means when you do something like this:

local dog = {15,20,6}
local cat = dog

Both dog and cat variables point to the same table so when you change something in dog table like dog[3] = 25 then when you try to print cat[3] it will output 25 on the console.

2 Likes

bruh that moment when you been learning how to script for 7 months and not knowing anything about references

It’s fine, we all do mistakes like that. Just know that string, number and boolean values are passed by value and tables, functions and userdatas are passed by reference.

1 Like

You are encountering the so-called weak and strong reference. What you are seeing here are strong references which the variables takes “ownership” of the value from the key. Weak references will change to accord of the other variable.

Although what you did was, not a reference. It was passing values. References are frequently mentioned on tables.

1 Like