Script won't Destroy() all ImageLabels using the GetChildren() Instance [SOLVED]

Hello! I have a whiteboard that I can paint on, which creates separate ImageLabels which can have different colours. However, I also have a clear button in SurfaceGui, which is supposed to clear all the ImageLabels from the board when it is clicked. It however, doesn’t work:

local children = script.Parent.Parent.Parent.Parent.SurfaceGui.Frame.Container:GetChildren()
script.Parent.MouseButton1Click:Connect(function()
	for i = 1, #children do
		children[i]:Destroy()
	end
end)

  • The Container is the frame which stores all the separate ImageLabels, when a player paints on the Whiteboard.

I have tried Youtube and other resources, but haven’t been able to resolve the problem.

Any help is appreciated!

This is my first ever topic, so if the topic is in the wrong category/sub-catecory, notify me and I will try and change it

2 Likes

Wait? Do you want it to only destroy one? Or all?

1 Like

I want it to destroy all, but I think I’ve tried it and it hasn’t destroyed any, but I will try it again.

1 Like

This should work!

script.Parent.MouseButton1Click:Connect(function()
local children = script.Parent.Parent.Parent.Parent.SurfaceGui.Frame.Container
	for i, v in ipairs(children:GetChildren()) do
		v:Destroy()
	end
end)

@Dfn150 :slight_smile: lol

2 Likes

Ok, I think you should change the script to this:

local children = script.Parent.Parent.Parent.SurfaceGui.Frame.Container:GetChildren()
script.Parent.MouseButton1Click:Connect(function()
    for paint, children in pairs(children) do
        children:Destroy()
        wait()
    end
end)

I think @Mikixxx0 already beat me, lol

1 Like

You might have spelled it wrong :sweat_smile:

No, ipairs is another form of a for loop

3 Likes

I will try both of the scripts and see if they work.

As @Dfn150 said ipairs is another form of a for loop

1 Like

Sorry, I’m still learning to code. :neutral_face:

2 Likes

Hmm…I have tried both of them but they aren’t doing anything, even though it is a script which is inside a textbutton.

Would using a clickdetector help?

The script should be a server script inside of the parent of all the paint

Yes true, it needs to be a server script

And like you said earlier, you may want to make it a click detector

The imagelabels are created inside the model, so would I have to change :GetChildren() if the script is in ServerScriptService? The script is currently in the textbutton of the clear button at the moment.

Server script service only executes arguments throughout the server. Since you already created the children variable, it must be inside of the model of the paint

It needs to be inside of the model

It is inside the model of the clear button at the moment. Is that where it should be?

wait, is that a local script or a serverscript?
that has to be a localscript.

1 Like

It’s currently a script. I did change it to a localscript with the old code but it didn’t change anything.