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?