WaitForChildOfClass

Common need: Find the humanoid of a character immediately when it becomes available after spawning.

Usual solution: WaitForChild.

humanoid = character:WaitForChild'Humanoid'

Why this is bad: Your code breaks if the humanoid isn’t named Humanoid, or if there’s another member of the character that happens to be named Humanoid. It’s ugly to make assumptions about name.

Solution: WaitForChildOfClass.

humanoid = character:WaitForChildOfClass'Humanoid'
103 Likes

Yeah. There should be equivalent “WaitForX” methods for all the new “FindFirstX” methods.

18 Likes

That’s fair. Would be a strange use-case to be sitting around waiting for a specific ancestor.

5 Likes

yes pls. I want this.

8 Likes

Yeah there have been times where I could use this (maybe quite a few lol) Support.

3 Likes

Makes sense, I think it’s good because other methods require more lines of code. This would make it more efficient.

1 Like

http://devforum.roblox.com/t/instance-waitforchild-string-name-int-timeout/

I’m happy they finally added a timeout parameter. It only took 2 years and a lot of arguing

2 Likes

Not to mention :WaitForChildWhichIsA() and :WaitForAncestor() and of the like would be great too.

5 Likes

While I do support this API, when I read the title of the thread I immediately of the bigger brain meme.

6 Likes

Since FindFirstChildOfClass() exists, it would make sense to add WaitForChildOfClass() too.

This would be useful, support!

10 Likes

This would be a very useful tool for us developers.

(I tried to use it just now and realized it didn’t exist… :sweat:)

5 Likes

I guess I work here now…
I’ll put it in my backlog.

20 Likes

I just really needed this and I’m disappointed that such an obvious feature wasnt added yet… :slightly_frowning_face:

It’s also surprising considering :FindFirstChildOfClass is a thing, I was expecting the same in this case. :thinking:

I think this really deserves to be added. :+1:

3 Likes

We decided against adding this, I’ll explain why.
Here were some of the obvious use cases:

  • Waiting for PlayerScripts to replicate
  • Waiting for Humanoids to replicate
  • Waiting for PlayerGui to replicate

You can see a theme where scripts are trying to use objects before they’re replicated–there are no guarantees that critical things will exist by the time scripts run.

There’s two ways address that:

  1. Make an engine-level feature to work around the lack of guarantees; add WaitForChildOfClass
  2. Get to the root of the problem and add those guarantees

Better to patch the boat than waste time bailing water, so we’re opting for the latter. This means more predictability and fewer edge cases in the long term.

Short term, you only need a few lines of code:

local function WaitForChildOfClass(parent, class)
	local child = parent:FindFirstChildOfClass(class)
	while not child or child.ClassName ~= class do
		child = parent.ChildAdded:Wait()
	end
	return child
end
36 Likes

In case anyone wants a version that works similar to the Roblox one with a single warning and an optional timeOut

function WaitForChildOfClass(parent,className,timeOut)
	local waitTime = 0
	local warned = false
	repeat
		local obj = parent:FindFirstChildOfClass(className)
		if obj then 
			return obj 
		else
			waitTime = waitTime + wait()
			if timeOut then 
				if waitTime > timeOut then return nil end
			elseif not warned then
				if waitTime > 5 then 
					warn("Infinite yield possible waiting on",parent:GetFullName() .. ":FindFirstChildOfClass(\"".. className .. "\")")
					warned = true
				end
			end
		end
	until false
end
27 Likes