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

How to detect if there are no children and perform a function?

So I have a doughnut machine, and the doughnuts are grouped for easier management. When the donuts are deleted, the group that holds them does not, which could probably lag servers after a few runs, how can I fix this without deleting donut groups that have donuts in them?

This is a GIF of what I mean.

EDIT: Forgot to mention I want to check each D1 group for children (as shown in gif) and not just the main group so I can delete groups without children that are in the main group.

1 comment

0

also sorry for no code examples, I didn't really know how to start.

4 answers

You can use an if statement as simple as this to check if an Instance has children.

if #instance:GetChildren() == 0 then -- // If there are NO children

end

1 comment

0

How do I do that for each D1 group as seen in the gif though?

To check if a model has no children, then you can simply do:


local children = model:GetChildren()

if #children >= 1 then
print('it has children')
else
print('it doesnt')
end

2 comments

0

How can I do the same but check all the D1 groups inside the main group/folder?

0

what do you mean by D1 groups? models named D1?

local checkChildren = coroutine.wrap(function()
	while wait(1) do
		for _,model in pairs(directory_to_the_parent_of_models:GetChildren()) do --// Change this to the path to the parent of all the models
			if #model:GetChildren() < 1 then
				print(#model:GetChildren())
			else
				print("No Children")
			end
		end
	end
end)

checkChildren() --// I made this a Coroutine so it runs in a new thread and doesn't stop any other code from being stopped with this while loop.

This will repeatedly loop through all the models in the model to check if it has 0 children or more than 0 children.

You could make this a function and call it whenever you want and customize it to look fancy or whatever you want.

Hope this helped! Feel free to select this as an answer if this helped you!

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