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()
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:
Good source
Very good source
Another good source
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)
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')
MyInstance:FAAAA()
I know it's hard to understand but if you think about it, you will understand it easily.
1 comment
I believe you'll have to use the Parent, or Model in this case, as an argument for your function