Hello everyone, it is with my great pleasure to announce my first module, FindSecondChild.
Ever wanted to use FindFirstChild but you thought it was goofy only letting the FIRST come, well now with FindSecondChild, it will find the second child!
USE CASES:
i dont know
API:
local FSC = require(FindSecondChild)
print(FSC:FindSecondChild(instance, childName)) --Prints the second child if found, if not returns nil.
The module is FREE and OPEN SOURCE for all to enjoy its glory! Have fun, but not too fun. I know you will be able to use this extremely POWERFUL tool; No credit is needed
-- Code has been simplified from the original
return function(instance, childName, number)
local child
local foundTimes = 0
for i, v in instance:GetChildren() do
if v.Name == childName then
foundTimes += 1
if foundTimes == number then
child = v
break
end
end
end
return child
end
In that case, could you give me a use case where I would need specifically the second child in place of the first, and what this module would achieve that iterating through children manually wouldn’t? I thought the order of instances obtained via :GetChildren/similar wasn’t guaranteed.
Well… I’m sure there’s at least one use case, but there would probably be an alternative anyway. I believe it’s not good practice to be scripting instances where there is multiple with the name name under the same parent. Like this:
Workspace
→ Part
→ Part
workspace.Part.Transparency = 0.5 -- There's two 'Part' objects. Which one is being changed?
Maybe the use case is for only two objects with the same name, to get both of them in only two lines instead of using a loop directly? But again, I never script objects like this, it’s just not reliable.
local function findSecondChild(part, name)
local objs = {}
for _, obj in pairs(part:GetChildren()) do
if obj.Name == name then
table.insert(objs, obj)
end
end
return objs[2]
end
--# Koyuki 2:39 PM 1/20/23
--/// @ Public Classes:
--# Table Functions:
local insert = table.insert;
local Module = {};
Module.__index = Module;
--/// @ Public Functions:
local GetInstanceProperty = function(ClassName: string): string?
return ClassName and (pcall(function() Instance.new(ClassName) end) and 'ClassName' or 'Name') or nil;
end;
--/// @ Public Metamethods:
function Module:WaitForChildOfClass(Object: Instance, ClassName: string, TimeOut: number?): Instance?
local Child = Object:FindFirstChildOfClass(ClassName);
if (Child) then return Child end;
repeat
Child = Object:FindFirstChildOfClass(ClassName);
task.wait();
until (Child) or (TimeOut and task.wait(TimeOut));
return Child;
end;
function Module:GetChildrenOfClass(Object: Instance, ClassName: string?): {Instance}
local Array = {};
for _, Child: Instance in (Object:GetChildren()) do
if (ClassName and Child[(GetInstanceProperty)(ClassName)] ~= ClassName) then continue end;
(insert)(Array, Child);
end;
return Array;
end;
function Module:ClearAllChildren(Object: Instance, ClassName: string?, TimeOut: number?): ()
task.wait(TimeOut or 0);
for _, Child: Instance in (typeof(Object) == 'Instance' and Object:GetChildren() or Object) do
if (not ClassName or Child[(GetInstanceProperty)(ClassName)] ~= ClassName) then continue end;
Child:Destroy();
end;
end;
--# Export:
return Module;
Just copy my code (that literally took me less than 10 minutes to write) and let’s move on with our day, jeez.
Next time I’ll make use of the irony / satire mark (⸮) when making satirical posts since there are people in the world who take “lol” and “xd” as face value.