Yielder X - A "WaitForChild()" alternative

What is Yielder X?
It is a module script made for personal use that acts as a customizable version of WaitForChild()

You can also search for multiple objects inside an instance simultaneously and yield a property until it isn’t nil (One use is for the player’s character since it is a property).

Model: YielderX - Roblox

How does it work?
Yielder X runs on a basic concept: If the object you’re trying to find doesn’t exist, it will wait a few seconds and try find it again, then repeats that process until it hits the maximum amount of attempts or until the object is no longer nil.

Once it does reach max attempts, it will terminate the yield and you can handle what happens when finding that object fails.

Usage:

-- Essential variables:
local yielderX = require( ... )
local objectToSearch = player.Character
local objectsToFind = {"HumanoidRootPart", "Left Leg"}

if yielderX.yield(objectsToFind, objectToSearch}) == nil then
    print("The objects have not been found.")
	-- You can decide how to handle it.
	return
end

print("Found HRP and Left Leg")

Practical Examples:
In this script, it will yield the player’s character, it’s humanoid and head, as well as a non-existent object.


Output:

Side notes:

  • If one of the objects in the table of things you’re trying to find is nil after the max attempt, it will outcome as nil for everything.
  • The things you want to search must be strings of their names in a table
2 Likes

WaitForChild already has a second TimeOut parameter, and in some of your examples you shouldn’t even be using it to yield to begin with!

local function OnCharacterAdded(Character: Model)
	local Humanoid: Humanoid? = Character:WaitForChild("Humanoid", 5) :: Humanoid
	local RootPart: BasePart? = if Humanoid then Humanoid.RootPart else nil
	
	if Humanoid and RootPart then
		...
	end
	
	if not RootPart then
		...
	end
end

Players.PlayerAdded:Connect(function(Player: Player)
	if Player.Character then
		OnCharacterAdded(Player.Character)
	end
	
	Player.CharacterAdded:Connect(OnCharacterAdded)
end)

Albeit there is still no built-in way to yield for a specific class so you have to make sure that your characters always load with their humanoids named “Humanoid” :frowning:

1 Like

I made a function specifically for that (has a timeout parameter aswell + infinite yield warning)