Retrieving an OOP class instance from an object in workspace

For context: I have a 3d grid building system. All placed items are represented as an instance of a custom oop class, which contains a couple of properties, and specifically one property that points to a model in Workspace (its ‘physical’ representation, if you will).

So with the custom oop class instance i can easily retrieve the model in workspace, but not the other way around.

  1. What do you want to achieve?
    I want to be able to retrieve the class instance from the Model in workspace.

  2. What is the issue?
    I am not aware of any way of binding a Model instance to a custom OOP class instance

  3. What solutions have you tried so far?
    I have tried looping over all placed items and checking if the model in that class instance is the one in workspace, which works. However for thousands of objects this is terrible for performance.

I hope ive made clear what im trying to achieve, let me know if its not.

You could use a table which uses the models as keys that point to the class instance values.

local instancesFromModelTable = {}

local model = workspace.TestModel
instancesFromModelTable[model] = {}

print(instancesFromModelTable[model]) --> table: <hex-code>
1 Like

I somehow never knew you could index tables by instances like that! I am going to try that, thanks

1 Like