function handleEquip(v)if v then if v.Parent~=char then hum:EquipTool(v)else hum:UnequipTools()end end end function create()local v=#inputOrder local f=v*iconSize.X.Offset+(v+1)*iconBorder.x local q=iconSize.Y.Offset+2*iconBorder.y frame.Size=UDim2.new(0,f,0,q)frame.Position=UDim2.new(.5,-(f/2),1,-(q+iconBorder.y*2))frame.Visible=true for v=1,#inputOrder,1 do local f=inputOrder[v]local q=template:Clone()q.Parent=frame q.Label.Text=f.txt q.Name=f.txt q.Visible=true q.Position=UDim2.new(0,(v-1)*iconSize.X.Offset+iconBorder.x*v,0,iconBorder.y)q.ImageTransparency=unequipped local z=f.tool if z then q.Tool.Image=z.TextureId end q.Tool.MouseButton1Down:Connect(function()for v,f in pairs(inputKeys)do if f.txt==q.Name then handleEquip(f.tool)end end end)end template:Destroy()end function setup()local v=bp:GetChildren()for f=1,#v,1 do if v[f]:IsA("\084\111\111\108")then for f=1,#inputOrder,1 do local q=inputOrder[f]if not q.tool then q.tool=v[f]break end end end end create()end function adjust()for v,f in pairs(inputKeys)do local q=f.tool local z=frame:FindFirstChild(f.txt)if q then z.Tool.Image=q.TextureId if q.Parent==char then z.ImageTransparency=equipped else z.ImageTransparency=unequipped end else z.Tool.Image=""z.ImageTransparency=unequipped end end end function onKeyPress(v)local f=v.KeyCode.Name local q=inputKeys[f]if q and uis:GetFocusedTextBox()==nil then handleEquip(q.tool)end end function handleAddition(v)if v:IsA("\084\111\111\108")then local f=true for q,z in pairs(inputKeys)do local W=z.tool if W then if W==v then f=false end end end if f then for f=1,#inputOrder,1 do local q=inputOrder[f].tool if not q then inputOrder[f].tool=v break end end end adjust()end end function handleRemoval(v)if v:IsA("\084\111\111\108")then if v.Parent~=char and v.Parent~=bp then for f=1,#inputOrder,1 do if inputOrder[f].tool==v then inputOrder[f].tool=nil break end end end adjust()end end

function handleEquip(tool)
	if tool then
		if tool.Parent ~= char then
			hum:EquipTool(tool)
		else
			hum:UnequipTools()
		end
	end
end

function create() -- creates all the icons at once (and will only run once)

	local toShow = #inputOrder -- # operator can only be used with an array, not a dictionary
	local totalX = (toShow*iconSize.X.Offset)+((toShow+1)*iconBorder.x)
	local totalY = iconSize.Y.Offset + (2*iconBorder.y)
	
	frame.Size = UDim2.new(0, totalX, 0, totalY)
	frame.Position = UDim2.new(0.5, -(totalX/2), 1, -(totalY+(iconBorder.y*2)))
	frame.Visible = true -- just in case!

	for i = 1, #inputOrder do
		
		local value = inputOrder[i]		
		local clone = template:Clone()
		clone.Parent = frame
		clone.Label.Text = value["txt"]
		clone.Name = value["txt"]
		clone.Visible = true
		clone.Position = UDim2.new(0, (i-1)*(iconSize.X.Offset)+(iconBorder.x*i), 0, iconBorder.y)
		clone.ImageTransparency = unequipped
		
		local tool = value["tool"]
		if tool then
			clone.Tool.Image = tool.TextureId
		end

		clone.Tool.MouseButton1Down:Connect(function() -- click icon to equip/unequip
			for key, value in pairs(inputKeys) do
				if value["txt"] == clone.Name then
					handleEquip(value["tool"]) 
				end 
			end
		end)

	end	
	template:Destroy()
end

function setup() -- sets up all the tools already in the backpack (and will only run once)
	local tools = bp:GetChildren()
	for i = 1, #tools do 
		if tools[i]:IsA("Tool") then -- does not assume that all objects in the backpack will be a tool (2.11.18)
		for i = 1, #inputOrder do
			local value = inputOrder[i]
			if not value["tool"] then -- if the tool slot is free...
				value["tool"] = tools[i]	
				break -- stop searching for a free slot
			end
		end
		end
	end
	create()
end

function adjust()
	for key, value in pairs(inputKeys) do
		local tool = value["tool"]
		local icon = frame:FindFirstChild(value["txt"])
		if tool then
			icon.Tool.Image = tool.TextureId
			if tool.Parent == char then -- if the tool is equipped...
				icon.ImageTransparency = equipped
			else
				icon.ImageTransparency = unequipped
			end
		else
			icon.Tool.Image = ""
			icon.ImageTransparency = unequipped
		end
	end
end

function onKeyPress(inputObject) -- press keys to equip/unequip
	local key = inputObject.KeyCode.Name
	local value = inputKeys[key]
	if value and uis:GetFocusedTextBox() == nil then -- don't equip/unequip while typing in text box
		handleEquip(value["tool"])
	end 
end

function handleAddition(adding)

	if adding:IsA("Tool") then
		local new = true

		for key, value in pairs(inputKeys) do
			local tool = value["tool"]
			if tool then
				if tool == adding then
					new = false
				end
			end
		end

		if new then
			for i = 1, #inputOrder do
				local tool = inputOrder[i]["tool"]
				if not tool then -- if the tool slot is free...
					inputOrder[i]["tool"] = adding
					break
				end
			end
		end

	adjust()
	end
end

function handleRemoval(removing) 
	if removing:IsA("Tool") then
		if removing.Parent ~= char and removing.Parent ~= bp then

			for i = 1, #inputOrder do
				if inputOrder[i]["tool"] == removing then
					inputOrder[i]["tool"] = nil
					break
				end
			end
		end

		adjust()
	end
end
