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

How can I make my script know when all children within a group are gone?

Asked by 3 years ago
Edited 3 years ago

I'm trying to make a collectible system that makes a sound when all the items within a group have been found. This is what I have...

local Notes = script.Parent.Notes local Sound = script.Parent.Sound

if Notes:GetChildren() == 0 then Sound:Play() end

Any help would be greatly appreciated!! :)

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

You would need to use the # operator. This operator gets the length of a table. Since GetChildren() returns a table we can simply put the # operator in front of Notes:GetChildren() so it would look like #Notes:GetChildren(). You can then check if the length of this table is equal to 0.

local Notes = script.Parent.Notes
local Sound = script.Parent.Sound

if #Notes:GetChildren() == 0 then
    Sound:Play()
end

Obviously the code above won't constantly check if the length of the table is equal to 0, so you would need to use a loop or an event to check. For example, you can use the ChildRemoved event so when something gets removed from "Notes" it will check if the length of the table is equal to 0.

local Notes script.Parent.Notes
local Sound = script.Parent.Sound

Notes.ChildRemoved:Connect(function()
    if #Notes:GetChildren() == 0 then
        Sound:Play()
    end
end)
0
well answered CaIcuIati0n 246 — 3y
0
That worked perfectly!! Thanks so much! antazma 8 — 3y
Ad

Answer this question