Viewport Frame toolbar Issue

I have been trying to make a viewport frame toolbar but there are a couple of issue. First off something that might be helpful to know is how I made the viewport frame work. I have a duplicate of the tool inside of the main tool and what it does is it clones the duplicate I have inside the main tool and moves into the viewport frame gui. I noticed that when I would drop the tool the viewport frame tool would delete and not come back. I don’t know why because it should duplicate the View frame tool when ever it is picked up.

I think that there is an issue with they way it is being cloned.

Here is the script(sorry for it being a mess)

if game:GetService("UserInputService").TouchEnabled ~= true then
	game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

	local CurrentSlotEquipped = ""

	--< variables >--
	local uis = game:GetService("UserInputService")
	local player = game.Players.LocalPlayer
	local char = workspace:WaitForChild(player.Name) -- added WaitForChild
	local bp = player.Backpack
	local hum = char:WaitForChild("Humanoid")

	local frame = script.Parent:WaitForChild("Frame")
	local inventory = script.Parent:WaitForChild("Inventory")
	local ScrollFrame =  inventory.ImageLabel.ScrollingFrame

	frame.Visible = true

	--<Properties>--
	local equippedTransparency = 0
	local unequippedTrnasparency = .5

	local Dragging = false

	local ItemDragged  =  nil
	local PasteItem = nil

	local mouse =  player:GetMouse()
	local MouseGui = script.Parent:WaitForChild("Template")


	local OwnedTools = {}

	table.insert(OwnedTools,1,"")
	for i = 1,5 do
		table.insert(OwnedTools,#OwnedTools+1,"")
	end
	local function SelectToolGui(except,reset,NewTool)
		for i,v in pairs(frame:GetChildren())do
			if v:IsA("ImageButton") then
				v:FindFirstChild("Selected").Visible = false
				v.Transparency = unequippedTrnasparency
				if v:IsA("ImageButton") then
					v.BackgroundTransparency = 1
					if v.Name == "1" or v.Name == "2" or v.Name == "3" or v.Name == "4" or v.Name == "5" or v.Name == "6" then
						v.Transparency = 1
					end
				end
			end
		end

		for i,v in pairs(ScrollFrame:GetChildren())do
			if v:IsA("ImageButton")then
				v:FindFirstChild("Selected").Visible = false
				if v:IsA("ImageButton") then
					v.BackgroundTransparency = 1
					if v.Name == "1" or v.Name == "2" or v.Name == "3" or v.Name == "4" or v.Name == "5" or v.Name == "6" then
						v.Transparency = 1
					end
				end
			end
		end

		if reset then
			CurrentSlotEquipped = ""
		else
			if CurrentSlotEquipped ~= except then
				if except <=6 then
					CurrentSlotEquipped = except
					script.Select:Play()
					local selectedPart = frame:FindFirstChild(tostring(except))
					selectedPart:FindFirstChild("Selected").Visible = true
					selectedPart.Transparency = equippedTransparency
					if selectedPart:IsA("ImageButton") then
						selectedPart.BackgroundTransparency = 1
					end
				else
					CurrentSlotEquipped = except
					script.Select:Play()
					local selectedPart = ScrollFrame:WaitForChild(tostring(except))
					selectedPart:FindFirstChild("Selected").Visible = true
				end
			elseif CurrentSlotEquipped == except then
				CurrentSlotEquipped = ""
			end
		end
		if  NewTool == nil then
			local ToolForEquipping = OwnedTools[CurrentSlotEquipped]
			script.Toolbar:FireServer(CurrentSlotEquipped,ToolForEquipping)
		end
	end

	local function ToolAdded(child,parent)
		if child:IsA("Tool") and table.find(OwnedTools,child) ==  nil then

			if table.find(OwnedTools,"")then
				local emptySpot = table.find(OwnedTools,"")
				table.remove(OwnedTools,emptySpot)
				table.insert(OwnedTools,emptySpot,child)
				if parent == char then
					SelectToolGui(emptySpot,nil,true)
				end
			else
				table.insert(OwnedTools,#OwnedTools+1,child)
				if parent == char then
					SelectToolGui(#OwnedTools,nil,true)
				end
			end
		end
	end

	local function CheckForTools()
		for i,v in pairs(bp:GetChildren())do
			ToolAdded(v,bp)
		end

		for i,v in pairs(char:GetChildren())do
			ToolAdded(v,char)
		end
	end

	local function ToolParentChanged()
		for i,v in pairs(OwnedTools)do
			if v ~= "" then
				if v.Parent ~= char and v.Parent ~= bp then
					table.remove(OwnedTools,i)
					if ScrollFrame:FindFirstChild(tostring(i))then
						ScrollFrame:FindFirstChild(tostring(i)):Destroy()
						for i,c in pairs(OwnedTools)do
							for a,b in pairs(frame:GetChildren())do
								if b.Name == tostring(i) then
									if c ~= "" then
										b.Item.Value = c
										for c,m in pairs(c:GetChildren())do
											if m:IsA("Tool") then
												m:Clone()
												m:Clone()
												m.Parent = b.ToolImage
											end
										end
									end
								end
							end
						end
					end
					table.insert(OwnedTools,i,"")
				end
			end
		end
	end

	for i,v in pairs(frame:GetChildren())do
		if v:IsA("ImageButton") then
			if v.Item.Value ~= nil then
				for m,b in pairs(v.ToolImage:GetChildren())do
					if b:IsA("Tool") then
						b:Destroy()
					end
				end
			else
				
			end
		end
	end
	CheckForTools()

	for i,v in pairs(frame:GetChildren())do
		if v:IsA("ImageButton") then
			v.Item.Changed:Connect(function()

			end)
			v.MouseButton1Click:Connect(function()
				if v.Item.Value ~= nil then
					if inventory.Visible ==  false then
						SelectToolGui(tonumber(v.Name))
					end
				end
			end)
		end
	end

	uis.InputBegan:Connect(function(Input,GP)
		if (GP) then return end
		if(Input.KeyCode == Enum.KeyCode.One) and frame["1"].Item.Value ~= nil then
			SelectToolGui(1)
		elseif(Input.KeyCode == Enum.KeyCode.Two) and frame["2"].Item.Value ~= nil then
			SelectToolGui(2)
		elseif(Input.KeyCode == Enum.KeyCode.Three) and frame["3"].Item.Value ~= nil then
			SelectToolGui(3)
		elseif(Input.KeyCode == Enum.KeyCode.Four) and frame["4"].Item.Value ~= nil then	
			SelectToolGui(4)
		elseif(Input.KeyCode == Enum.KeyCode.Five) and frame["5"].Item.Value ~= nil then
			SelectToolGui(5)
		elseif (Input.KeyCode == Enum.KeyCode.Backquote) then

			inventory.Visible = not inventory.Visible

			if inventory.Visible ==  true then
				script.Open:Play()
			end
		elseif (Input.KeyCode == Enum.KeyCode.Backspace) then
			SelectToolGui(1,"true")
		end
	end)

	char.ChildAdded:Connect(function(child)
		ToolAdded(child,char)
	end)

	bp.ChildAdded:Connect(function(child)
		ToolAdded(child,bp)
	end)


	local function upDateToolBar()
		for i,v in pairs(OwnedTools)do
			for a,b in pairs(frame:GetChildren())do
				if b.Name == tostring(i) then
					if v ~= "" then
						b.Item.Value = v
						for v,m in pairs(v:GetChildren())do
							if m:IsA("Tool") then
								m:Clone()
								m:Clone()
								m.Parent = b.ToolImage
							end
						end
					else
						b.Item.Value = nil
						for m,b in pairs(b.ToolImage:GetChildren())do
							if b:IsA("Tool") then
								b:Destroy()
							end
						end
					end
				end
			end
		end
	end


	local function CreateButtonForTool(i,v)
		local template = inventory.ImageLabel.ScrollingFrame.Template

		local NewButton =  template:Clone()
		NewButton.Parent = template.Parent
		NewButton.Visible =  true
		NewButton.Name = tostring(i) 
		
		

		NewButton.MouseButton2Click:Connect(function()
			if table.find(OwnedTools,"")then
				local NewEmptySpot = table.find(OwnedTools,"")

				if NewEmptySpot < 6 then
					script.Switch:Play()
					table.remove(OwnedTools,NewEmptySpot)
					table.insert(OwnedTools,NewEmptySpot,v)
					table.remove(OwnedTools,i)
					table.insert(OwnedTools,i,"")
					NewButton:Destroy()
				end
			end
		end)


		local gui =  NewButton	

		gui.InputBegan:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton1 and inventory.Visible == true and ItemDragged == nil and Dragging == false  then
				Dragging =  true
				gui.Visible = false

				ItemDragged = tonumber(gui.Name)
				MouseGui.Visible = true
	
			end
		end)		
		gui.InputEnded:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton1 and Dragging == true and ItemDragged == tonumber(gui.Name) then
				Dragging =  false
				gui.Visible = true
			end
			
				gui:FindFirstChild("Selected").Visible =  false
				gui.Transparency = 1
			MouseGui.Visible = false


				if PasteItem ~= nil then

				if PasteItem ~= inventory then
						local PasteItemIndex = tonumber(PasteItem.Name)
						table.insert(OwnedTools,PasteItemIndex,OwnedTools[ItemDragged])
						table.remove(OwnedTools,ItemDragged+1)
						table.insert(OwnedTools,ItemDragged+1,OwnedTools[PasteItemIndex+1])
					table.remove(OwnedTools,PasteItemIndex+1)
					for i,v in pairs(OwnedTools)do
						for a,b in pairs(frame:GetChildren())do
							if b.Name == tostring(i) then
								if v ~= "" then
									b.Item.Value = v
									for v,m in pairs(v:GetChildren())do
										if m:IsA("Tool") then
											m:Clone()
											m:Clone()
											m.Parent = b.ToolImage
										end
									end
								end
							end
						end
					end
					gui:Destroy()
				end
				ItemDragged = nil
			end
		end)
	end


	inventory.MouseEnter:Connect(function()
		PasteItem = inventory
	end)

	inventory.MouseLeave:Connect(function()
		PasteItem = nil
	end)

	for i,gui in pairs(frame:GetChildren()) do
		if gui:IsA("ImageButton")then

			gui.MouseEnter:Connect(function()
				PasteItem = gui

			end)
			gui.MouseLeave:Connect(function()
				if PasteItem == gui then
					PasteItem = nil
				end
			end)
			
			gui.MouseButton2Click:Connect(function()
				if gui.Item.Value ~= nil and inventory.Visible == true then
					local guiPlace = tonumber(gui.Name)
					
					if OwnedTools[6] == ""then
						table.remove(OwnedTools,6)
						table.insert(OwnedTools,6,OwnedTools[guiPlace])
						
						if CurrentSlotEquipped == tonumber(gui.Name) then
							SelectToolGui(true,true)
						end


					elseif OwnedTools[6] ~= "" then
						local Once =  false
						for i,v in pairs(OwnedTools)do
							if i > 6 and Once == false then
								if v == "" then
									Once = true
									table.remove(OwnedTools,i)
									table.insert(OwnedTools,i,OwnedTools[guiPlace])	
									if CurrentSlotEquipped == tonumber(gui.Name) then
										SelectToolGui(true,true)
									end

									break
								end

							end
						end

						if Once == false then
							table.insert(OwnedTools,#OwnedTools+1,OwnedTools[guiPlace])
						end
					end
					table.remove(OwnedTools,guiPlace)
					table.insert(OwnedTools,guiPlace,"")
				end
			end)

			gui.InputBegan:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 and inventory.Visible == true and ItemDragged == nil and Dragging == false and gui.Item.Value ~= nil then
					Dragging =  true
					gui.Visible = false

					ItemDragged = tonumber(gui.Name)
					MouseGui.Visible = true

				end
			end)		
			gui.InputEnded:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 and Dragging == true and ItemDragged == tonumber(gui.Name) then
					Dragging =  false
					gui.Visible = true
					gui:FindFirstChild("Selected").Visible =  false
					gui.Transparency = unequippedTrnasparency
					MouseGui.Visible = false

					if PasteItem ~= nil then
						if PasteItem == inventory then
							if OwnedTools[6] == ""then
								table.remove(OwnedTools,6)
								table.insert(OwnedTools,6,OwnedTools[ItemDragged])
								
								if CurrentSlotEquipped == tonumber(gui.Name) then
									SelectToolGui(true,true)
									
								end

							elseif OwnedTools[6] ~= "" then
								local Once =  false
								for i,v in pairs(OwnedTools)do
									if i > 6 and Once == false then
										if v == "" then
											Once = true
											table.remove(OwnedTools,i)
											table.insert(OwnedTools,i,OwnedTools[ItemDragged])
											if CurrentSlotEquipped == tonumber(gui.Name) then
												SelectToolGui(true,true)
												
											end
											break
										end

									end
								end

								if Once == false then
									table.insert(OwnedTools,#OwnedTools+1,OwnedTools[ItemDragged])
								end
							end
							table.remove(OwnedTools,ItemDragged)
							table.insert(OwnedTools,ItemDragged,"")
						else
							local PasteItemIndex = tonumber(PasteItem.Name)

							if ItemDragged < PasteItemIndex then
								table.insert(OwnedTools,PasteItemIndex,OwnedTools[ItemDragged])
								table.remove(OwnedTools,ItemDragged)
								table.insert(OwnedTools,ItemDragged,OwnedTools[PasteItemIndex])
								table.remove(OwnedTools,PasteItemIndex+1)
								if CurrentSlotEquipped == ItemDragged then
									SelectToolGui(PasteItemIndex,nil,true)
								end

							else
								table.insert(OwnedTools,PasteItemIndex,OwnedTools[ItemDragged])
								table.remove(OwnedTools,ItemDragged+1)
								table.insert(OwnedTools,ItemDragged+1,OwnedTools[PasteItemIndex+1])
								table.remove(OwnedTools,PasteItemIndex+1)
								if CurrentSlotEquipped == ItemDragged then
									SelectToolGui(PasteItemIndex,nil,true)
								end


							end

						end
					end

					ItemDragged = nil
				end
			end)
		end
	end


	local function DragGui()		
		script.Parent.Template.Position = UDim2.new(0,mouse.X - 10,0,mouse.Y + 30)
	end

	local function upDateInventory()
		for i,v in pairs(OwnedTools)do
			if  ScrollFrame:FindFirstChild(tostring(i)) ==  nil then
				if i>5 and v ~= "" then
					CreateButtonForTool(i,v)
				end
			else
				if v ~= ""then
					ScrollFrame:FindFirstChild(tostring(i)).ToolName.Text = v.Name
				else
					ScrollFrame:FindFirstChild(tostring(i)).ToolName.Text = ""
				end
			end
		end
	end
	game:GetService('RunService').Heartbeat:Connect(function()
		DragGui()
		ToolParentChanged()
		upDateToolBar()
		upDateInventory()

	end)
end

This hurts my eyes to look at.
Two things you can do to make this cleaner:

  1. Utilize “and” for example the lines:
    if b.Name == tostring(i) then
    if c ~= “” then
    goes to
    if b.Name == tostring(i) and c ~= “”

  2. Simpler conditionals where your not doing anything if its false you can do:
    if v == “” then break end
    though using breaks like that can get messy if you use to many.