Questions about "self"

Hi, I recently created a library module that replaces legacy Roblox functions with faster ones. It works, but I recently discovered the self, and tried to implement it, but I’m still confused.

I don’t know if this is possible, but I would like to do something like:

workspace:WaitChild('Baseplate')

-- Instead of:

Module:WaitChild(workspace, 'Baseplate')

The Module

Well, the module itself is very simple, but I will show here only one function.

Original Function
--> Custom WaitForChild()
local RunService = game:GetService("RunService")
local Stepped = RunService.Stepped

local Module = {}

function Module:WaitChild(ChildParent, ChildName, Time)
	assert(ChildParent, ':WaitChild(ChildParent, ChildName, Time) -- ChildParent is nil.') --> Will return error if not ChildParent
	assert(typeof(ChildName) == 'string', ':WaitChild(ChildParent, ChildName, Time) -- ChildName is not a valid string.') --> Will return error if ChildName isn't a string
	
	Time = Time or 5 --> If time is nil, then Time = 5
	Time = Time <= 0 and 5 or Time --> If Time is negative, then  Time = 5
	
	local RemainingTime = Time
	local Found = false
	local Child
	
	while RemainingTime >= 0 do --> Will work like wait()
		RemainingTime = RemainingTime - Stepped:Wait()
		
		Child = Module:FindChild(ChildParent, ChildName) --> Custom FindFirstChild
	
		if Child and ChildName == Child.Name then
		--> If the Child was found, then stop and return Child
			Found = true
			break
		
		elseif RemainingTime == 0 then
		--> If the time ended, then stop searching
			Found = false
			break
		end
	end
	
	if Found == true then
		return Child
	else
		return Found
	end
end
Function with "self"
--> Custom WaitForChild()
local RunService = game:GetService("RunService")
local Stepped = RunService.Stepped

local Module = {}

function Module:WaitChild(ChildName, Time)
	assert(typeof(ChildName) == 'string', ':WaitChild(ChildParent, ChildName, Time) -- ChildName is not a valid string.') --> Will return error if ChildName isn't a string
	
	Time = Time or 5 --> If time is nil, then Time = 5
	Time = Time <= 0 and 5 or Time --> If Time is negative, then  Time = 5
	
	local RemainingTime = Time
	local Found = false
	local Child
	
	while RemainingTime >= 0 do --> Will work like wait()
		RemainingTime = RemainingTime - Stepped:Wait()
		
		Child = Module:FindChild(self, ChildName) --> Custom FindFirstChild
	
		if Child and ChildName == Child.Name then
		--> If the Child was found, then stop and return Child
			Found = true
			break
		
		elseif RemainingTime == 0 then
		--> If the time ended, then stop searching
			Found = false
			break
		end
	end
	
	if Found == true then
		return Child
	else
		return Found
	end
end

I was pretending to use the self function like:

workspace:WaitChild('Baseplate')

But it apparently does not work.


What I’m doing wrong? Is this possible?

The thing is when you do a:b(c), you’re really doing a.b(a, c). Module:WaitChild(workspace, 'BasePlate') is essentially passing Module as its first argument, which is bad design on its own. It’s not really possible to replace the native instance functions without wrapping instances, which would probably lose any performance benefit you might have wanted.

Also, looking at the implementation, you’re better off using the native Roblox functions. With the new VM optimizations they are guaranteed to run faster than pretty much anything you can make in Lua. Calls to instances have been greatly optimized, so this is not really an issue or something you should worry about.

4 Likes