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

oh i know why it wasn’t working you’re getting the children when the game first starts.

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

or you can use ClearAllChildren() instead

local Path = script.Parent.Parent.Parent.Parent.SurfaceGui.Frame.Container
script.Parent.MouseButton1Click:Connect(function()
Path:ClearAllChildren()
end)
1 Like

It needs to be a local script if you’re keeping it inside of the text button

No need to overcomplicate your code. This should do it.

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

Don’t get the children once the script starts as not all instances (or none at all) might’ve loaded in the game, get them when you need them (in this case once the player presses the button).

1 Like

I get this error:

“SurfaceGui is not a valid member of PlayerGui”

put the whole path inside the Event

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

or

script.Parent.MouseButton1Click:Connect(function()
local Path = script.Parent.Parent.Parent.Parent.SurfaceGui.Frame.Container
Path:ClearAllChildren()
end)
1 Like

Yes do it as @DEVLocalPlayer said, it should work [Replied to the wrong person]

No, you must put :WaitForChild because PlayerGui is not a member of anything until the client runs

already mentioned that up there :smiley:

when the player will click on the button the gui will be already loaded.

But why then is PlayerGui a child of SurfaceGui?

Yes you did, but @DEVLocalPlayer said the code first, you said a different code: :slight_smile: Don’t want to be rude!

1 Like

yes its a different code both will work what’s the difference?

@Herbz_Dev2106, has really nothing worked still?

1 Like

I still have the same error, but I think I now have another error from a different script, expecting it to be a script instead of a localscript.

no it has to be a localscript don’t change it to a script.
if you’re interacting with guis you must use localscripts.

1 Like

The difference is that:
@DEVLocalPlayer’s Code updates the “Paint” every time the player clicks the button, BUT your code updates the “Paint” 1 time when the game loads :smiley: Not trying to be rude to you

Currently at the moment, I get the previous error and another error from another script which is expecting it to be a script when it is a localscript, I think.

"Script is not a valid member of TextButton “Players.Herbz_2106.PlayerGui.Draw.Tools.Clear”

Is the local script a child of the text button?

The textbutton is currently a parent of the localscript.