Way to link part to a table (object)

You see, I want to be able to “link” a part to a table. Instead of looking through my array of all the objects and matching the part to each object’s own part, I’d prefer to just have something as simple as “Part.Object”. Since the objects are all tables, I can’t exactly do this with things like values.

1 Like

I don’t believe there is a way to reference a value from a table directly without using its index, but you can use table.find to easily get a certain value from a table. table.find uses a linear search algorithm, as you do when you loop through a table, but is much simpler to use.

Table = {"hello", "yeet", "random string"}
ValueToFind = "random string"
index = table.find(Table, ValueToFind) -- this will return it's index in the table, which is 3 in this case. 

--once you have the index, you can easily reference it with Table[index]

Not exactly sure if this is what you were looking for, but I hope it helps regardless.

1 Like

Iterating over a array in lua is fast, you could iterate over thousands of items in less than a second, are you sure your not pre-optimizing because using a dictionary will not be any faster.

Today while searching something up, I came across this post for Object Values. I have never used them before but you can take a look if you want.
ObjectValue | Documentation - Roblox Creator Hub
I think you can then set a table value = to the name of the object value.
Idk…

ObjectValues are essentially Studio “pointers”. They store the location of something in Studio.

So you just want a custom property for a part? You could wrap the instance.

local function wrap_instance(instance, extra_properties)
    return setmetatable({ self = instance }, { __index = instance })
end

local part = Instance.new("Part")
part.Parent = workspace
part = wrap_instance(part, { Object = { } })
print(part.Object)
3 Likes

Thanks, this will definitely help with my project.

I’ve got two questions about this old but accepted answer…

(1) In wrap_instance, shouldn’t “extra_properties” be used as the 2nd param on metatable? If not, how does extra_properties get linked to the instance? I.e.,:

local part = Workspace.Maps.Map4.TwrBases.TwrBase1
local function wrap_instance(instance, extra_properties)
    return setmetatable({ self = instance }, { __index = instance })
end
part=wrap_instance(part, { Object = "t" }) -- Later, twr will contain reference to a Tower instantiated class object
print(part.Object) -- causes "Object is not a valid member of Part".

So, it should be:

local part = Workspace.Maps.Map4.TwrBases.TwrBase1
local function wrap_instance(instance, extra_properties)
    return setmetatable({ self = instance }, { __index = extra_properties})
end
part=wrap_instance(part, { Object = "t" }) -- Later, twr will contain reference to a Tower instantiated class object
print(part.Object) -- Prints "t", yay.

(2) How do we keep the ‘part’ acting as a part? Above, the ‘part’ becomes a table. For example,

part=wrap_instance(part, { Object = "t" })
print(part.Object) -- Prints "t", yay.

-- later, I want to parent a different thing to the part (works WITHOUT the wrapper) but once wrapped, throws an error:
local ovTorch = Instance.new("ObjectValue")
ovTorch.Parent = part   -- causes “invalid argument #3 (Instance expected, got table)”.