#Important question

1 messages · Page 1 of 1 (latest)

stable comet
#

Can someone please explain to me how do i use [] while coding? With examples please

steel sequoia
#

Examples:
getting a certain spot in a table

local example = {"A", "B", "C"}
example[1] -- returns A
example[2] -- returns B

finding a child of an instance with a variable

local instance = script.Parent
local instance2 = instance[variable] -- looks for an instance with the same name as the variable, if found returns the instance else returns nil

they can be used in tables to get info by putting in a variable

local profit = {
  ["Jake"] = "5$"
  ["Bella"] = "10$"
}
local money = profit["Jake"] -- returns 5%
lilac jewel
#

with [] you can index tables. its incredibly helpful. example above explains it perfectly

naive dew
#

it can be anything

#

could be a function

#

metatables

#

anything

lilac jewel
#

Another useful feature with [] is you can index properties of an Instance.
Let's say u make a Utility module about properties or Instances:

function Utils.setProperty(instance: Instance, property: string, value: any)
   instance[property] = value
end

steel sequoia
#

i havent needed it ig

lilac jewel
#

only thing is, if you are in !strict mode, the linter may be annoying about it. to fix it, just do this:

(instance :: any)[property] = value