Getting a reference to a CoreObject

I am struggling to understand how to, or to find an answer to, reference a Core Object from a script when the Object does not exist yet.

Example:

I want to spawn in some Core Objects and add those Core Objects to a list then I want to cycle through those Core Objects in that list and preform manipulations to those objects.

I felt like it would have something to do with Tables but I am struggling to find documentation on Tables and how to add Core Objects to a table in script.

If anyone knows where I can look to learn about how to do this, or guide me in the right direction to finding the answers, I would be grateful.

World.SpawnAsset(string assetId, [table parameters])

Returns a reference to the CoreObject spawned.

So if you want to keep track of the object in a table the you can do something like this:

local objectList = {}
local objectReference = World.SpawnAsset(string assetId, [table parameters])
table.insert(objectList, objectReference)

See documentation for an example:

1 Like

If I am understanding correctly, then I could use

local otherScript = require( script:GetCustomProperty("SecondScript") )

for _, object in pairs(otherScript.objectList) do
    object:DoSomething()
end
    

to call the DoSomething() function of each object in the list that is in the other script?

Edit: Okay, I just tried this and realized it doesn't do what I thought it did.

How do I access that table from another script?

Edit:

I managed to do it by making the objectList global.. not sure if that's the best way to do it but it worked.

_G.objectList = {}