Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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.

0
also sorry for no code examples, I didn't really know how to start. VoidKeyword 111 — 4y

4 answers

Log in to vote
0
Answered by
Alphexus 498 Moderation Voter
4 years ago
Edited 4 years ago

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
0
How do I do that for each D1 group as seen in the gif though? VoidKeyword 111 — 4y
Ad
Log in to vote
0
Answered by
Subsz 366 Moderation Voter
4 years ago
Edited 4 years ago

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
0
How can I do the same but check all the D1 groups inside the main group/folder? VoidKeyword 111 — 4y
0
what do you mean by D1 groups? models named D1? Subsz 366 — 4y
Log in to vote
0
Answered by 4 years ago
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!

Log in to vote
0
Answered by 4 years ago

LOLOLOL<LOLLOLO YOU

Answer this question