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

How would I loop through a model?

Okay, so I was trying to make a script that will make a flood happen and when you die you spawn somewhere else. However, it will not work. Could someone help me?




local flood_control = script.Parent:WaitForChild("flood_controller")
local flood = script.Parent
local spawnP = game.Workspace:WaitForChild("MainPlate").SpawnLocation
local sound = script.Parent:WaitForChild("Part"):WaitForChild("Intense Music 4")

while wait(.5) do
	if flood.Parent == workspace then
		flood_control.Disabled = false
		spawnP.Enabled = false
	else
		if flood.Parent ~= workspace then
			flood_control.Disabled = true
			spawnP.Enabled = true
		end
	end
end





then I tried again and did this...




local flood_control = script.Parent:WaitForChild("flood_controller")
local flood = script.Parent
local spawnP = game.Workspace:WaitForChild("MainPlate").SpawnLocation
local sound = script.Parent:WaitForChild("Part"):WaitForChild("Intense Music 4")

for _,v in pairs(flood) do
	if v.Parent == workspace then
		spawnP.Enabled = false
		flood_control.Disabled = false
	else
		if v.Parent ~= workspace then
			spawnP.Enabled = true
			flood_control.Disabled = true
		end
	end
end



and it still didn't work :(

9 comments

0

What does this have to do with looping through a model?

0

I put this script into a model

0

ok, but where in this script do you need to loop through the model and why?

0

Ok, in your second attempt, you can't check to see if parts in a model are parented to the workspace because you know where they are, they are in the model...

0

As for looping through the model, that second attempt, answers your question

0

ok, so how do I know and why won't it work

0

what do you mean by how do you know? It will do exactly what you tell it to do, there will be no errors in this unless flood doesn't actually exist.

0

for _, v in pairs(flood:GetChildren())*

0

Do that and it will work for your second script, SteamG0D doesn't know what he's talking bout...

1 answer

✓ Accepted answer
for _,child in pairs(model:GetChildren()) do
	-- do thing with child
end

Will loop over the first-generation children.

for _,descendant in pairs(model:GetDescendants()) do
	-- do thing with descendant 
end

Will loop over the entire contents of the model (all instances inside, no matter how deep will be visited) because GetDescendants() recursively traverses the model hierarchy and builds an array of everything inside.

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