I’ve got a series of parts, called Plinths, around the map. During the game, they get objects assigned to them, from a class called clTwrs. Later on, I want to check if a particular Plinth has a class assigned or not. The class (a table) has the Plinth as its parent. So, to check if the Plinth has a class, I should just use :GetChildren(). But, :GetChildren() isn’t showing the class as a child.
Here’s the simplified class module and the code that invokes it:
function Twr.new(pPlayer, pTypeStr, pParentPlinth)
local lNewTwr = {} -- Will return the new Twr
setmetatable(lNewTwr, Twr) -- For making Twr work like a class
lNewTwr.Name = "TwrObj" -- I tried setting .Name to see if that made a printable name to appear in :GetChildren()
lNewTwr.Player = pPlayer
lNewTwr.TypeStr = pTypeStr
lNewTwr.Parent = pParentPlinth
return lNewTwr
end
-- and the code the invokes it:
ClTowers = require(game.ReplicatedStorage.clTwr)
local lNewTower = ClTowers.new(lPlayer, "TowerType1", lPlinth)
-- This all works; the tower and all its parts are created by the class module.
-- But
print(lPlinth:GetChildren()) -- does not list the Table which has the Plinth as its parent
-- Trying again, but manually setting parent
local lNewTower = ClTowers.new(lPlayer, "TowerType1", lPlinth)
lNewTower.Parent=lPlinth -- This is done in the class, but double-checking by doing it here too
print(lPlinth:GetChildren()) -- still doesn't list it
Note the line in .new that sets the new object’s parent to the plinth. In case there was a problem with that not working because the object was still in the process of being created, I added a 2nd attempt to set its parent, outside of the .new method. In either case, it doesn’t appear as a child.
How do I get access to the child table using things like :WaitForChild(“put something here to refer to Table”)?
Things that haven’t worked:
(1) Add a Custom Property to a Part. There must be a way to have a custom property of a part, which points to a Table. However, it’s not so easy in Lua. It’s the same problem as Way to link part to a table (object) .
(2) Using an ObjectValue parented to the Plinth; this worked when pointing the OV at a model, and I can use :GetFirstChild(“OVTwrObject”) to get to the Parts, but, doesn’t accept Tables. Why it’s limited to not do that, I’ve no idea. And it’s the Table I need, because it’s got all the state properties.
(3) Using a Config object as a child of the Plinth with a whole load of custom properties. This is how it started, but it doesn’t scale well and is very cumbersome - i.e. I’m writing an entire “this is how you get and set properties” in a class, getting it to create its own Config child objects… it was like creating OOP methods from scratch, which is obviously the wrong path, but what is the right way to simply link a part to its class data?