Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to make another :FindFirstChild() ?

Asked by 4 years ago
Edited 4 years ago

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.

0
I believe you'll have to use the Parent, or Model in this case, as an argument for your function Spjureeedd 385 — 4y

1 answer

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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:

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)
    -- 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.

0
Yeah you almost got it, i edited it with little more detais imKirda 4491 — 4y
0
ah i see i see very good work you did Heavenlyblobmaster 271 — 4y
Ad

Answer this question