Help with plugin acting like a part is till selected?

I have a plugin and for some reason when I select a new model it acts like both are selected?

cln.BG.Add.Activated:Connect(function()
	cln.BG.Camera.Visible = false
	cln.BG.View.Visible = false
	for i, v in pairs(cln.BG.ViewportFrame:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
	local selection = game.Selection:Get()[1]
	if selection then
		if selection:IsA("Model") then

If you don’t deselect the other model and then select the new one, the old one will still be index 1 in your Selection:Get(). If you only care about the most recent selection you could try something like:

local fullSelection = game.Selection:Get()
local selection = fullSelection[#fullSelection]

…which will use the most recently selected item in your Selection:Get() result.

1 Like

Still does not work:
full script:

db2 = false
cln.BG.Add.Activated:Connect(function()
	cln.BG.Camera.Visible = false
	cln.BG.View.Visible = false
	for i, v in pairs(cln.BG.ViewportFrame:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
	local fullSelection = game.Selection:Get()
	local selection = fullSelection[#fullSelection]
	if selection then
		if selection:IsA("Model") then
			local clone = selection:Clone()
			clone.Parent = cln.BG.ViewportFrame
			cln.BG.Camera.Visible = true
			cln.BG.View.Visible = true
			cln.BG.Light.Visible = true
			cln.BG.FOV55.Visible = true
			cln.BG.FOV70.Visible = true
			cln.BG.line.Visible = true
			cln.BG.LightDelete.Visible = true
			cln.BG.CameraDelete.Visible = true

			cln.BG.Camera.Activated:Connect(function()
				if selection:FindFirstChild("ThumbnailCamera") then
					selection:FindFirstChild("ThumbnailCamera"):Destroy()
				end
				local camera = Instance.new("Camera")
				camera.Name = "ThumbnailCamera"
				camera.CFrame = game.Workspace:WaitForChild("Camera").CFrame
				camera.Parent = selection
				local camclone = camera:Clone()
				camclone.Parent = cln.BG.ViewportFrame
				cln.BG.ViewportFrame.CurrentCamera = camclone
			end)

			cln.BG.View.Activated:Connect(function()
				if db2 == false then
					db2 = true
					local ui = script.ViewUI:Clone()
					ui.Parent = game:WaitForChild("CoreGui")
				else
					db2 = false
					if game:WaitForChild("CoreGui"):FindFirstChild("ViewUI") then
						game:WaitForChild("CoreGui"):FindFirstChild("ViewUI"):Destroy()
					end
				end
			end)

			cln.BG.FOV70.Activated:Connect(function()
				game.Workspace.Camera.FieldOfView = 70
			end)

			cln.BG.FOV55.Activated:Connect(function()
				game.Workspace.Camera.FieldOfView = 55
			end)

			cln.BG.Light.Activated:Connect(function()
				if selection.PrimaryPart then
					if selection:FindFirstChild("PointLight") then
						selection:FindFirstChild("PointLight"):Destroy()
					end
					local light = script.PointLight:Clone()
					light.Parent = selection.PrimaryPart
				else
					error("MUST HAVE PrimaryPart to use this feature!")
				end
			end)

			cln.BG.LightDelete.Activated:Connect(function()
				if selection.PrimaryPart then
					if selection.PrimaryPart:FindFirstChild("PointLight") then
						selection.PrimaryPart:FindFirstChild("PointLight"):Destroy()
					end
				end
			end)
			cln.BG.CameraDelete.Activated:Connect(function()
				if selection:FindFirstChild("ThumbnailCamera") then
					selection:FindFirstChild("ThumbnailCamera"):Destroy()
					if cln.BG.ViewportFrame:FindFirstChild("ThumbnailCamera") then
						cln.BG.ViewportFrame:FindFirstChild("ThumbnailCamera"):Destroy()
					end
				end
			end)
			while true do
				wait(0.1)
				if selection.PrimaryPart:FindFirstChild("PointLight") then
					cln.BG.ViewportFrame.Ambient = Color3.new(0.807843, 0.807843, 0.807843)
				else
					cln.BG.ViewportFrame.Ambient = Color3.new(0.435294, 0.435294, 0.435294)
				end
			end
		end
	end
end)

Use the SelectionChanged event to find out if an object has been selected/deselected.

Well in the script it only should change when you press the add button