Issue with Selection:Get

I’ve been scripting a simple plugin, which basically just converts TextLabels into something like an ImageLabel, but I’ve encountered an issue where Selection:Get returns nil instead of what is selected.

However, the weird thing is that sometimes it works and sometimes it doesn’t. And earlier, the things that are returning nil when selected, worked just fine.

Why is this happening?

[edit] - i’ve found that if you get the same textlabel or whatever, parent it so something completely random, like replicatedstorage, Selection:Get works as intended. something about the location of the selected thing is affecting Selection:Get i think.

Code:

local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")


local toolbar = plugin:CreateToolbar("UI Converter")


local texttoimage = toolbar:CreateButton("Convert to ImageLabel", "Convert to ImageLabel", "rbxassetid://12987379773")
--local texttobutton = toolbar:CreateButton("Convert to ImageButton", "Convert to ImageButton", "rbxassetid://12987381043")
--local uitolabel = toolbar:CreateButton("Convert to TextLabel", "Convert to TextLabel ", "rbxassetid://12987382199")


ChangeHistoryService:SetEnabled(true)
local destroyoldBool = script:WaitForChild("destroyold").Value

local sgui = script:WaitForChild("Converter")
local convframe = sgui.ConvFrame
local toIL = convframe.toIL
local toIB = convframe.toIB
local toTL= convframe.toTL


texttoimage.Click:Connect(function()
	if sgui.Parent == script then
		sgui.Parent = game:WaitForChild("CoreGui")
	elseif sgui.Parent == game:WaitForChild("CoreGui") then
		sgui.Parent = script	
	end
end)

texttoimage.ClickableWhenViewportHidden = true
--texttobutton.ClickableWhenViewportHidden = true
--uitolabel.ClickableWhenViewportHidden = true

local function onTTIClicked()
	local selectedObjects = Selection:Get()
	print(selectedObjects[1])
	ChangeHistoryService:SetWaypoint("Button Pressed")
	if #selectedObjects == 1 then
		--if selectedObjects[1].ClassName == "ImageLabel" or selectedObjects[1].ClassName == "TextLabel" or selectedObjects[1].ClassName == "ImageButton" then
		if selectedObjects[1]:isA("GuiLabel")  or selectedObjects[1]:isA("GuiButton") then
			ChangeHistoryService:SetWaypoint("Changing UI to ImageLabel")

			local new = Instance.new("ImageLabel")
			new.Parent = selectedObjects[1].Parent
			new.Size = selectedObjects[1].Size
			new.Position = selectedObjects[1].Position
			new.Transparency = selectedObjects[1].Transparency
			new.BackgroundColor3 = selectedObjects[1].BackgroundColor3
			new.Name = selectedObjects[1].Name

			for _, child in pairs(selectedObjects[1]:GetChildren()) do
				child.Parent = new
			end

			new.Visible = selectedObjects[1].Visible
			new.Rotation = selectedObjects[1].Rotation
			new.BackgroundTransparency = selectedObjects[1].BackgroundTransparency
			new.ZIndex = selectedObjects[1].ZIndex
			new.Selectable = selectedObjects[1].Selectable
			new.BackgroundColor = selectedObjects[1].BackgroundColor
			new.SelectionOrder = selectedObjects[1].SelectionOrder
			new.BorderColor3 = selectedObjects[1].BorderColor3
			new.BorderColor = selectedObjects[1].BorderColor
			new.BorderSizePixel = selectedObjects[1].BorderSizePixel
			if destroyoldBool == true then
				selectedObjects[1]:Destroy()	
			end
		end
	end
	ChangeHistoryService:SetWaypoint("Changed UI to ImageLabel")  

end

local function onTTBClicked()
	local selectedObjects = Selection:Get()
	ChangeHistoryService:SetWaypoint("Button Pressed")

	if #selectedObjects > 0 and #selectedObjects < 2 then
		--if selectedObjects[1].ClassName == "ImageLabel" or selectedObjects[1].ClassName == "TextLabel" or selectedObjects[1].ClassName == "ImageButton" then
		if selectedObjects[1]:isA("GuiLabel")  or selectedObjects[1]:isA("GuiButton")  or selectedObjects[1]:isA("GuiBase2d") then
			ChangeHistoryService:SetWaypoint("Changing UI to ImageButton")
			local new = Instance.new("ImageButton")
			new.Parent = selectedObjects[1].Parent
			new.Size = selectedObjects[1].Size
			new.Position = selectedObjects[1].Position
			new.Transparency = selectedObjects[1].Transparency
			new.BackgroundColor3 = selectedObjects[1].BackgroundColor3
			new.Name = selectedObjects[1].Name

			for _, child in pairs(selectedObjects[1]:GetChildren()) do
				child.Parent = new
			end

			new.Visible = selectedObjects[1].Visible
			new.Rotation = selectedObjects[1].Rotation
			new.BackgroundTransparency = selectedObjects[1].BackgroundTransparency
			new.ZIndex = selectedObjects[1].ZIndex
			new.Selectable = selectedObjects[1].Selectable
			new.BackgroundColor = selectedObjects[1].BackgroundColor
			new.SelectionOrder = selectedObjects[1].SelectionOrder
			new.BorderColor3 = selectedObjects[1].BorderColor3
			new.BorderColor = selectedObjects[1].BorderColor
			new.BorderSizePixel = selectedObjects[1].BorderSizePixel
			if destroyoldBool == true then
				selectedObjects[1]:Destroy()	
			end
		end
	end
	ChangeHistoryService:SetWaypoint("Changed UI to ImageButton")
end

local function onUTLClicked()
	local selectedObjects = Selection:Get()
	ChangeHistoryService:SetWaypoint("Button Pressed")
	
	if #selectedObjects > 0 and #selectedObjects < 2 then
		--if selectedObjects[1].ClassName == "ImageLabel" or selectedObjects[1].ClassName == "TextLabel" or selectedObjects[1].ClassName == "ImageButton" then
		if selectedObjects[1]:isA("GuiLabel")  or selectedObjects[1]:isA("GuiButton")  or selectedObjects[1]:isA("GuiBase2d") then
			--		ChangeHistoryService:SetWaypoint("Changing UI to TextLabel")
			local new = Instance.new("TextLabel")
			new.Parent = selectedObjects[1].Parent
			new.Size = selectedObjects[1].Size
			new.Position = selectedObjects[1].Position
			new.Transparency = selectedObjects[1].Transparency
			new.BackgroundColor3 = selectedObjects[1].BackgroundColor3
			new.Name = selectedObjects[1].Name

			for _, child in pairs(selectedObjects[1]:GetChildren()) do
				child.Parent = new
			end

			new.Visible = selectedObjects[1].Visible
			new.Rotation = selectedObjects[1].Rotation
			new.BackgroundTransparency = selectedObjects[1].BackgroundTransparency
			new.ZIndex = selectedObjects[1].ZIndex
			new.Selectable = selectedObjects[1].Selectable
			new.BackgroundColor = selectedObjects[1].BackgroundColor
			new.SelectionOrder = selectedObjects[1].SelectionOrder
			new.BorderColor3 = selectedObjects[1].BorderColor3
			new.BorderColor = selectedObjects[1].BorderColor
			new.BorderSizePixel = selectedObjects[1].BorderSizePixel
			if destroyoldBool == true then
				selectedObjects[1]:Destroy()	
			end
		end
	end
	ChangeHistoryService:SetWaypoint("Changed UI to TextLabel")

end

toIL.MouseButton1Click:Connect(onTTIClicked)
toIB.MouseButton1Click:Connect(onTTBClicked)
toTL.MouseButton1Click:Connect(onUTLClicked)


are you getting any errors? also, does it break on a specific function or all of them?

It breaks on all the functions, and doesn’t give me any sort of error.

I’m not entirety sure then, all I know is in my plugins I just use game.Selection:Get() and don’t use a variable for Selection if you wanna try that but idk

1 Like

Nope. That doesn’t seem to work either. Thanks though.

upon pressing the button to convert, anything that is selected gets deselected, thus returning nil. silly mistake on my behalf, sorry (only happens for things like frames and labels)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.