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

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

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

✓ Accepted answer

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)

2 comments

0

well answered

0

That worked perfectly!! Thanks so much!

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