Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 9 days ago
Edited 9 days 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 9 days ago
Edited 9 days 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 163 — 9d
0
That worked perfectly!! Thanks so much! antazma 6 — 8d
Ad

Answer this question