How to Check if a GUI Object has a Child

I’m trying to make a plugin that when you select a TextButton, it puts a UICorner in it. To check if the player has selected the TextButton, I’m running a while wait() do (which is probably not the best of things to do, so please help me find a different way).

Since it’s just a continious loop of seeing if the player has selected the button, when they do, it spawn lots and lots of UICorner's in it because of the loop.

How do I make my script know that there is already a child that is a UICorner so it doesn’t spawn more in?

Script:

while wait() do
	for _, object in pairs(Selection:Get()) do
		if object:IsA("TextButton") then
			local Corner = Instance.new("UICorner")
			Corner.Parent = object
			Corner.CornerRadius = UDim.new(0.2,0)
		end
	end
end

You could hook onto the SelectionChanged event.

i.e:

Selection.SelectionChanged:Connect(function()
	for _, object in pairs(Selection:Get()) do
		if object:IsA("TextButton") then
			local Corner = Instance.new("UICorner")
			Corner.Parent = object
			Corner.CornerRadius = UDim.new(0.2,0)
		end
	end
end)

You can #table and if it’s higher than zero, say it contains something

if (#object:GetChildren() >= 1) then

This does work slightly since when you select the TextButton it only puts one UICorner in, but if you reselect the button it puts another one in etc.

Oh, right.

You could probably use object:FindFirstChildWhichIsA to check if it has a UICorner already.

i.e.

if not object:FindFirstChildWhichIsA("UICorner") then
    Instance.new()... etc.
end
1 Like

That goes back to spawning lots of the UICorner's again.

Also, would there be a better way for a loop instead of while wait() do?

Show me the code snippet after your changes.

local Selection = game:GetService("Selection")

Selection.SelectionChanged:Connect(function()
	for _, Object in pairs(Selection:Get()) do
		if Object:IsA("TextButton") then
			if not Object:FindFirstAncestorWhichIsA("UICorner") then
				local Corner = Instance.new("UICorner")
				Corner.Parent = Object
				Corner.CornerRadius = UDim.new(0.2,0)
			end
		end
	end
end)

Yes, as I wrote above using SelectionChanged

You used FindFirstAncestor instead of FindFirstChildWhichIsA

Thank you! It worked.

Also, is there a way how to count how many GUIObjects a player is selecting? I’ve seen other plugins do it.

You could get the selection, then iterate over it with a for loop and remove any non-GuiObjects.

local sel = Selection:Get()
for k, v in pairs(sel) do
    if not v:IsA("GuiObject") then
        sel[k] = nil
    end
end

Where would I put this in my script?

local Selection = game:GetService("Selection")

Selection.SelectionChanged:Connect(function()
	for _, Object in pairs(Selection:Get()) do
		if Object:IsA("TextButton") then
			if not Object:FindFirstChildWhichIsA("UICorner") then
				local Corner = Instance.new("UICorner")
				Corner.Parent = Object
				Corner.CornerRadius = UDim.new(0.2,0)
			end
		end
	end
end)

Where you would like to get how many guiobjects a player is selecting.

I’m really confused where to put this. I’ve tried multiple times.
It now only says Select a GUI Object (0).

local Selection = game:GetService("Selection")

Selection.SelectionChanged:Connect(function()
	for _, Object in pairs(Selection:Get()) do
		if Object:IsA("TextButton") then
			if not Object:FindFirstChildWhichIsA("UICorner") then
				for i, v in pairs(Selection:Get()) do
					if not v:IsA("GuiObject") then
						Selection:Get()[i] = nil
						script.Parent.Subtitle.Text = "Select a GUI Object ("..i..")"
					end
				end
				local Corner = Instance.new("UICorner")
				Corner.Parent = Object
				Corner.CornerRadius = UDim.new(0.2,0)
			end
		else
			script.Parent.Subtitle.Text = "Select a GUI Object (0)"
		end
	end
end)

Oh sorry, no remove it.

That was to count how many gui objects there were. In this case, you don’t need it.

My idea was if you select multiple GUIObjects, it will make a counter.

Starts off as:
image

When you select things:
image

image

And them it puts the UICorner in all three of those buttons.

Yeah, but do that outside the for-loop. It should not be related to the UICorner code.

Make sure that when you remove the item, you are removing it from a variable, not from Selection:Get()

Change this:

for i, v in pairs(Selection:Get()) do
    ...
    Selection:Get()[i] = nil
    .... -- other stuff here
end

To this:

local sel = Selection:Get()
for i, v in pairs(sel) do
    ...
    sel[i] = nil
    .... -- other stuff here
end
script.Parent.Subtitle.Text = "Select a GUI Object ("..#sel..")"

Doesn’t work, I’m probaly doing something wrong.

Sorry if this is annoying btw, I’m new to plugins!

local Selection = game:GetService("Selection")

Selection.SelectionChanged:Connect(function()
	for _, Object in pairs(Selection:Get()) do
		if Object:IsA("TextButton") then
			if not Object:FindFirstChildWhichIsA("UICorner") then
				local Corner = Instance.new("UICorner")
				Corner.Parent = Object
				Corner.CornerRadius = UDim.new(0.2,0)
			end
		else
			script.Parent.Subtitle.Text = "Select a GUI Object (0)"
		end
	end
	local Sel = Selection:Get()
	for i, v in pairs(Sel) do
		if not v:IsA("GuiObject") then
			Sel[i] = nil
			script.Parent.Subtitle.Text = "Select a GUI Object ("..i..")"
		end
	end	
end)