Wrappers
Because ScriptGuider is just one of those people who likes to miss the point by half a mile, let me explain. When you want to add custom methods to something like a Roblox Instance, you have to get into the hacky world of wrappers. The reason for this is simple - You can't change how the Roblox classes behave, but you can change how everything else treats them.
An example wrapper is simple enough - It's a small table which pretends that it's a Part. Note that it is not a part, and that this kind of implementation is incredibly naïve and has several pitfalls, but should be sufficient for you to learn the fundamental concepts from.
01 | local FakePartClass = { } ; |
02 | local function newFakePart(Part) |
03 | return setmetatable ( { _self = Part } ,FakePartClass); |
05 | function FakePartClass:Explode() |
06 | Instance.new( "Explosion" , self).Position = self.Position |
09 | FakePartClass.__index = function (t,k) |
10 | local v = FakePartClass [ k ] or t._self [ k ] ; |
11 | if type (v) = = 'function' then |
12 | return function (_,...) |
13 | return v(t._self, ...) |
Did you notice the problems? There are a few - Other things won't treat this like a real Part, so you can't use it as a function argument. All member functions are expected to be called as methods, which is fine in this case. All children of the Part will not have the same methods. There are also several other issues but I won't detail them here.
Want a full wrapper to toy with? Valkyrie has a pretty heavy one which doesn't have all the same flaws as other primitive solutions.