Help: Locating All Descendants of a Certain Name

I have a mass amount of NPCs in a Folder (named “ClapAvis”), all with Animations in them (named “ClapAnim”) and want to have a script where all ClapAnims are played for each NPC in unison. In less jargonistic terms, I wanna make all my NPCs clap at the same time.

I have attempted to get it to find the Animations by name, that were each inside Groups within the Folder (hence use of GetDescendants):

local claps = game.Workspace.ClapAvis:GetDescendants()

for i = 1, 3 do
	if claps.Name == "ClapAnim" then
		print("hooray it worky")
	end
	print("if only this, no worky")	
end

The only output received is if only this, no worky 3 times (no confusion in this quantity). Suffice to say, no worky. It also fails when the ClapAnim is in the root of the Folder.

I want to be able to get it to be able to locate these animations, and then play all of these at the same time without having a separate line for each individual one. It’ll be triggered by a ClickDetector, but that’s further down the track. I need the function itself to work upon running before putting it to a trigger.

claps is a table so in your loop you need to reference the index:

if claps[i].Name == "ClapAnim" then

In it’s current state your loop will obviously only check the first 3 items in the table so if the animations are further down the list then it won’t find them.
To check the whole folder descendants:

for _, file in ipairs(game.Workspace.ClapAvis:GetDescendants()) do
	if file.Name == "ClapAnim" then
		print("hooray it worky")
	end
	print("if only this, no worky")	
end
1 Like

Thanks for your help! I was able to re-tool my script with the advice you gave, and got everything working:

local clappers = game.Workspace.ClapAvis:GetDescendants()

for _, clappers in ipairs(game.Workspace.ClapAvis:GetDescendants()) do
	if clappers.Name == "Clapper" then
		local clapAni = clappers:WaitForChild("ClapAnim")
		local clapProper = clappers.Humanoid:LoadAnimation(clapAni)
		clapProper:Play()
		clapProper:AdjustSpeed(2.5)
		print("worky")
		wait(0.05)
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.