Understanding of Tables with "[]"

Hello, I have been trying to understand the square brackets (used for tables) and I have come across a line of code that completely made me confused.

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				print("Obj,k= "..obj[k])
				obj[k] = v
				print(obj, k, obj[k])
			end
		end
		return obj
	end
end

Create("Part"){
	Name = "New_Part",
	Parent = workspace
}

The line of code is the obj[k]

And my question is why is it indexing k in obj? and what does it mean?

3 Likes

k is a key in the dictionary whereas v is the value connected to the specific key. When using a dictionary, you can index a value within it using a key assigned to the value itself.

2 Likes

Say your table is {"String1", "String2", "String3", True}

for k, v in pairs(data) do
in this scenario let’s say this is our third iteration, our k variable would = 3 and our v variable would = “String3”
this is useful for many things but since i see your using (data) i would assume you’re looking at a table datastore Script.

Note: arrays/tables/lists/logs usually start with [0] as the first index but in Lua we start with [1]

Oh, no the data was the “custom” properties for the table in the return function line.

But thanks a lot for explaining!

2 Likes

Generally speaking, assuming that obj is a dictionary, setting obj[k] = v will assign any given value v to a specific key k within obj. The key can be any number, string or object.

This enables you to store or read any given value under a specific key within a table, as demonstrated by the short example script below:

local Table = {}
local k = "this key is a string" -- number, string or object 
local v = "this value is a string" -- any value
Table[k] = v
print(Table)

--CONSOLE OUTPUT: >> {
--["this key is a string"] = "this value is a string"
--}

This only covers a small portion of the true power of dictionaries, you can read more about them on the Creator Hub.

However, in the context of the script you provided, obj is not defined as a table, but rather as an instance. This is not really an issue, since part of the semantics that apply to tables also apply to instances. In some situations, you can simply view them as dictionaries where each property is stored under its respective name. Your script does not index a table, but takes a dictionary of properties and sets each property (named k) of the newly created instance (of type ty) to the given value v.

1 Like

It confuses me when you use square brackets next to the obj which is an Instance

I like to think of tables as working how a coat-check service works.

local value = "Coat"
local index = "VitalWinter"
local t = {}
t[index] = value

print(t["VitalWinter"]) --prints "Coat"

You give the table your your coat (value) and tell it your name (index).

The name (index) doesn’t have to be a string. It could be a number, an object, or anything really. It can be anything because it will just be used as a reference to get you your coat (value) back again.

3 Likes

It’s simply an alternative to setting properties directly. Instead of doing

local BasePart = Instance.new("BasePart")
BasePart.Anchored = true
BasePart.Transparency = 1
BasePart.CanCollide = false

you can also write

local BasePart = Instance.new("BasePart")
BasePart['Anchored'] = true
BasePart['Transparency'] = 1
BasePart['CanCollide'] = false

or utilize a table:

local BasePart = Instance.new("BasePart")
local Properties = {
   Anchored = true,
   Transparency = 1,
   CanCollide = false
}

for key, value in pairs(Properties) do
BasePart[key] = value
end
1 Like

Try this script and look at the output … it will become clear.

local mytable = {"String1", "String2", "String3"}
print(mytable)
1 Like

That makes a lot of sense! Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.