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

Local scripts don’t execute when placed inside the ‘Workspace’ container.

1 Like

Is that in the textbutton?

I think I am still getting the same error, annoyingly.

Does the directory actually exist?

PlayerGui exists inside the player, yes. If that is what you meant.

You are defining the children when the script first runs, and there is nothing there, you need to re-define the children every time you want to delete them.
Use this

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

I have updated the script. :+1:

1 Like

All I can think of is something like this

ServerScript

Remote.OnServerEvent:Connect(function()
	for _, Child in next, Children do
		if Child:IsA('ImageLabel') then
			Child:Destroy()
		end
	end
end)

Local

script.Parent.MouseButton1Click:Connect(function()
Remote:FireServer()
end)
1 Like

I will try that now, and see if a RemoteEvent works better.

It will run on server once your clear button is clicked. I don’t know if you want it on client or server but if you want it to show for everyone when clicked clear then remotevents are your best bet

Would the LocalScript be under the RemoteEvent?

It can be anywhere but I would prefer to put it under the remote yes.

I get this error: "MouseButton1Click is not a valid member of RemoteEvent".

Oh then change script.Parent to the TextButton that you click to clear

1 Like

It doesn’t seem to recognise ‘Children’, on the second line.

Also, would it be better if I put the script inside SurfaceGui, so I don’t have to find the name of the model it is associated with?

Make a variable called Children and the variable contents will be GetChildren like this

local Children = SurfaceGui:GetChildren()

and ofcource replace SurfaceGui with where all the image labels are where you want to destroy

It would most likely be better yes.

I think your scripts work fine, but I think I have another error from another script called ‘CopyDraw’ on line 22 which is preventing it from working properly for some reason:

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(char)
		local copy = script.Parent.Draw:Clone()
		copy.Parent = player.PlayerGui
		copy.Adornee = script.Parent
		if player:GetRankInGroup(7108062) >= 23 then
			copy.Enabled = true
			copy.BoardLoc.Value = script.Parent
			copy.ImageButton.LocalScript.Disabled = false
		else
			copy.Enabled = false
			copy.ImageButton.LocalScript.Disabled = true
		end
		
		
		
		
		for i,v in pairs(copy.Tools:GetChildren()) do
			if v.Name == "CurrentDot" then
				v.Dot.Change.Disabled = false
			else
				v.Clear.Destroy_Event.LocalScript.LocalScript = false --this is where the error is
			end
		end
	end)
end)

CopyDraw Script

Is “Clear” under TextButton? or no

The LocalScript is under a RemoteEvent, which is in the TextButton.

Edit: now I don’t get any errors from the ‘CopyDraw’ script, but it doesn’t clear the ImageLabels. Going to investigate for a second.