Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I don't understand how using brackets on tables can mean inserting?

local tab = {}

local key = "Potatoes"
local value = "are cool"

tab[key] = value

-- or you could do:
tab.Potatoes = "are cool"
-- or
tab["Potatoes"] = "are cool"

print(tab[key]) -- prints: are cool
print(tab.Potatoes) -- prints: are cool
print(tab["Potatoes"]) -- prints: are cool


for key, value in pairs(tab) do
    print(key, value)
end

-- since we have a "Potatoes" key with "are cool" as its value, it would print:
-- Potatoes are cool

on line 6 it saids tab[key] = value

but i dont understand what this does

Its like inserting but I dont understand how this inserts

shoudl't doing this change tabs key into value which is 'are cool'

but it dosen't it prints Potatoes are cool and merges it

1 answer

This is because it adds an index to the table, it sets the index to the table

This discussion is preserved as it was. New answers, comments, and votes are closed.