Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to GetChildren() of many parts at once?

Very messy but i do not now how to write "Spawn", i want to have all the spawn0 - spawn6 in a single line

	local Spawn0 = game.Workspace.Map.Normal.spawner:GetChildren()
	local Spawn1 = game.Workspace.Map.Normal.spawner1:GetChildren()
	local Spawn2 = game.Workspace.Map.Normal.spawner2:GetChildren()
	local Spawn3 = game.Workspace.Map.Normal.spawner3:GetChildren()
	local Spawn4 = game.Workspace.Map.Normal.spawner4:GetChildren()
	local Spawn5 = game.Workspace.Map.Normal.spawner5:GetChildren()
	local Spawn6 = game.Workspace.Map.Normal.spawner6:GetChildren()
	local Spawn = (Spawn0, Spawn1 , Spawn2, Spawn3, Spawn4, Spawn5, Spawn6) --?

the reason im wanting all in spawn is for the following line:

for i,model in pairs(Spawn) do
		local enemyHuman = model:FindFirstChildWhichIsA("Humanoid")

so the zombies spawn in game.Workspace.Map.Normal.spawner, game.Workspace.Map.Normal.spawner1, game.Workspace.Map.Normal.spawner2... etc. and i need it all gathered for this turret im making

2 answers

local zombieNum = 0 -- to make the difference
local spawns = {}
for _, spawn in pairs(workspace.Map.Normal:GetChildren()) do
	for _, zombie in pairs(spawn:GetChildren()) do
		if zombie:FindFirstChildWhichIsA("Humanoid") then
			spawns["zombie"..zombieNum] = zombie
		end
	end
end

2 comments

0

not really what im looking for, im adding context in the post to further explain

0

ok im updating

By using a for loop, this should be relatively easy.

First, we grab the parent of all the spawners, in this case, workspace.Map.Normal.

Then we put it into a for loop, looping through everything inside that parent, searching for Humanoids, since I assume only zombies have that inside workspace.Map.Normal. This can be accomplished with :GetDescendants(), which gets the child, the child of the child, the child of the child of the child, etc. under the instance.

After that, put it into a table.

In case new zombies spawn, we want to add them to the table too, so whenever a descendant is added, we add them to the table. Whenever a descendant is destroyed/removed, we take them out of the table.

Now the zombies table will contain any zombies spawned and is still alive.

local zombies = {}

for _, v in pairs(workspace.Map.Normal:GetDescendants()) do
	if v:IsA(‘Humanoid’) then
		zombies[v.Parent] = true --puts the model the humanoid is in into a table
	end
end

workspace.Map.Normal.DescendantAdded:Connect(function(descedant)
	if descendant:FindFirstChildWhichIsA(‘Humanoid’) then --if it’s a zombie
		zombies[descendant] = true
	end
end)

workspace.Map.Normal.DescendantRemoving:Connect(function(descendant)
	zombies[descendant] = nil --if the descendant is in the table, it will be removed, else this wont really do anything
end)

Quick edit: This table is a dictionary, so in a for loop, you need to use the index part, not the value part.

--example
for zombie, v in pairs(zombies) do
	--zombie is the actual instance, and v is just a placeholder
end

This discussion is preserved as it was. New answers, comments, and votes are closed.