What am I trying to achieve?
Using OOP to assign information onto a Basepart, such a model.
What is the issue?
I’m trying to store information of one item, in the same way as you would add String and Bool values in a Folder under a model- just in the way of using coding.
I’m unsure and somewhat confused how I can get around this.
What have I attempted?
I’ve tried to use metatables using the .new function
EDIT:
I’m trying to avoid declaring a variable when using the .new function
Metatables are fun and all, but you should try looking into this method of applying properties. The way I do it is I have a lookup table that holds all the other data that I want for the instance.
A very-basic example:
local t = {}
function MakeProperties(instance,properties)
local val = t[instance]
if not val then
val = properties or {}
t[instance] = val
end
return val
end
function GetProperties(instance)
return t[instance]
end
Essentially you’re just looking for a place to hold properties and this is the very basis of doing that. If you need to get an instance’s properties just call the second function, otherwise if it returns nil you should make the properties.
local partProps = GetProperties(myPart) or MakeProperties(myPart,{
CustomProperty1 = true,
AnotherProperty2 = "SomeValue"
})
print(partProps.CustomProperty1)
Not only is this less quirky since it’s not using metatables, but it’s pretty simplistic and you don’t have to think about edge cases. It’s as simple as setting and getting properties.
Some things to note when doing something like this:
Remember to collect garbage! If a model gets destroyed you need to remove the reference within that master “t” table, otherwise you’ll be collecting (leaking) memory over time and that’s not good.
You’ll probably want to split the GetProperties function into a GetProperty and SetProperty function respectively. It’s bad practice to hand over the entire properties table, otherwise you could potentially do something to that table that the system did not expect or does not check for.