Creating a WaitForChildWhichIsA function

I want to make a WaitForChildWhichIsA function

It attempts to call a nil value on this line:
image

I have tried youtube.com and developer.roblox.com

My custom functions module 2.0 attempts to call a nil value on self:GetDescendants() and I really do not know much more to say other than I am trying to call it with a variable I do not know how to use self so good by the way sorry if this sucks
game.Workspace.Baseplate

--ServerScript
local OOP_Script = require(script["OOP-Script"]) --// Please read the OOP-Script before using this
local Recursives = require(script.Recursives)
local MainModule = require(script.Parent)
local MainFunctions = require(script.MainFunctions)

local part = game.Workspace.Baseplate
part = MainFunctions:WaitForChildWhichIsA("ClickDetector")

print(MainModule.Variables.Thank_You)

--ModuleScript
local mainFunc = {}

function mainFunc:WaitForChildWhichIsA(class)
	
	local descendants = self:GetDescendants()
	
	if class and #descendants > 0 then
		
		local findClass = self:FindFirstChildWhichIsA(class)
		
		if findClass then repeat wait() until findClass elseif not findClass then wait(2) warn("Infinite Yield possible on " .. "'" .. findClass .. "'") end
		
		return "Found Object"
	end
end

return mainFunc

2 Likes

I would love to get any help you could give and I am trying my hardest so you know :grinning_face_with_smiling_eyes:

you’re not passing the part to the function, you’re passing the module to the function.

when you do:

MainFunctions:WaitForChildWhichIsA(class)  --this is the same as typing 
MainFunctions.WaitForChildWhichIsA(MainFunctions,class) --this
1 Like

Yeah but I do not want to have a second parameter I want only one where it finds it and I know I should self for a ‘:’ function on a module

Since I made the WaitForChildWhichIsA the . one will not work

You’re not quite grasping the problem here.
You have a “part” that you want to find a decendent of.
But you’re not running “WaitForChild…” on the part, you’re running it on the module.
the part doesn’t get checked for anything at all.

Your code might as well say this:

local part = game.Workspace.Baseplate
part = nil

Because the “MainFunctions:WaitForChildWhichIsA()” function isn’t being passed the part at all.

1 Like

How do I make it pass the part without using a second param?

It sounds like what you’re trying to achieve is something like this:

local part = game.Workspace.Baseplate
part:WaitForChildWhichIsA()

You will NOT be able to do this unless you either A) Somehow modify the default roblox API to extend the functions that are inherited by roblox’s “Part” object. or B) Create your own new type of Part object and give it this function and then instantiate it.

Yes it is !!! :grinning_face_with_smiling_eyes:

I do not know how I am not so good on coding :frowning:

You’re going to just have to get over the idea of using an extra variable.
Passing the part in to a function is the best way to do this.

Your code would look like this instead:

local part = game.Workspace.Baseplate
local _type = "Click Detector" -- you can't use "type" as a variable as it already exists in the API namespace.
local part2 = MainFucntions.WaitForChildWhichIsA(part,_type)

Oh ok :sob: Well this kinda sucks because I am trying to make my own functions like this:

part:ChangeSurface()
or
part:GetProperties()
or
part:WaitForChildWhichIsA()-- but is it not possible?

you can’t do part:WaitForChildWhichIsA() without paramaters

I know but is it possible to create my own function functions like this:

local part = workspace.Part

part:WaitForChildOfClass("ClickDetector")

It is possible, but you have to go about it a bit differently.

Those functions “GetProperties” and “ChangeSurface” are hardcoded into the Roblox API, the only way to add your own function of this type is by extending the actual Part object in the roblox API or by creating your own version of part that has the functions you want in it.

You can do the later with something called a Factory Pattern. I think the functionality you’re looking for right now is a bit beyond the scope of what you’re trying to do, for at least the level you’re at. But you’re not wrong in wanting to do it that way, it’s just going to be easier for you to do it another way for now most likely.

Well how do I make my own part with functions?

Woudl I do something like this?

local part = require(script.Module) and workspace.Part

Would It then have the original functions and the module ones?

Try to keep one response at a time, this isn’t a chat, it’s a forum, other people will come and read this and by spamming multiple responses you edge out other people’s post. Just update your last post if you need to add more information or wait and type it all in one post.

But to answer your question, you need to do something called a Factory Pattern.
This will allow you to create a module which can make your own parts which have the functionality you desire.

1 Like

Try this:

function mainFunc.GetClass(class)
    local object = objectYouWantToGetTheChild:WaitForChildWhichIsA(class)
end