So I am making a script that makes it easier to use the default functions. You can access these functions by requiring the module script first
local f = require(module) f.math.random(10,20,10) -- math.random() but with a precision component f.Instance.new("Part",workspace,{{"Name","NewPart"},{"Size",Vector3.new(1,1,1)}})
You get the idea. Anyways, as all of you know, Instances have a special function called FindFirstChild. I was wondering if there was any way to make a function like that using the colon. It would look something like this:
Model:f.Instance.FFC("Part") -- just like Model:FindFirstChild()
Where the model that calls the function is able to be accessed within the function.
You can't do it like you want but Metatables could work in you case very good because you have custom Instance.new
so in your function you would do something like
function Instance.new(Class) local self = { Parent = Instance.new(Class) } return setmetatable(self, YourMetatable') end
If you do not understand metatables, read sources that i will link under the questions because metatables are very very confusing sometimes because of trashy documentation and noobs using it terribly wrong so i'll drop really legit sources. So to the function, with metatable you would just make function in the metatable like this:
function YourMetatable:FindFirstChild('Name') local Parent = self.Parent local Children = Parent:GetChildren() --/This will loop through the parent's children and look for --\child's name if i understood the question wrong and you --\you don't know how to make custom FindFirstChild for _, Child in ipairs(Children) do if (Child.Name == Name) then return Child end end end
But this function will be not understandable for you if you don't as i said earlier metatables
. Here are the good sources for learning them that i found:
Yup you kind of get it but it looks bit different because in your case self is not usable, so here is where you need to use __index
metamethod, in your function Instance.new
you would have to do this
function Instance.new(something) local self = {} return setmetatable(self, MyMetatable) end
This is what is showed but metatable will look like this:
local MyMetatable = {} MyMetatable.__index = MyMetatable function MyMetatable:FFC(Name) -- la la la end
Now why __index
? Because when setting the metatable in Instance.new
function, the tablee you set to it will be connected to the __index
too because it's in that metatable so let's say you do:
local MyInstance = f.Instance.new() MyInstance:FFC('bla bla bla')
This would not work because FFC
is not a member of the table you created in Instance.new
function BUT it is part of the MyMetatable
and since __index
fires when it can't find given index in this
table, it will look into the __index
variable inside the metatable and since we set the __index
to the metatable itself, it will look into the metatable and look for function named FFC
and if it finds it, it will return it. So example of full short script:
local MyMetatable = {} MyMetatable.__index = MyMetatable function MyMetatable:FFC(Name) print(Name) print(self.Parent) end function Instance.new(something) local self = { Parent = Instance.new(something) } return setmetatable(self, MyMetatable) end local MyInstance = Instance.new(something) MyInstance:FFC('Cats') -- This will print out [Cats] and [something] (The instance that was created in Instance.new MyInstance:FAAAA() -- This will error since FAAAA is not part of this table and not even -- part of the metatable this table is connected to
I know it's hard to understand but if you think about it, you will understand it easily.