-- Feel free to use however you want
-- Credit is appreciated but not necessary 



return function(SlotsFolder,overflowCollapse)
	
	overflowCollapse = overflowCollapse or true
	
	-- Define some values
	local UserInputService = game:GetService("UserInputService")
	local Players = game:GetService('Players')
	local Player = Players.LocalPlayer
	local EquippedVal = Instance.new('ObjectValue')
	EquippedVal.Name = 'Equipped'
	if not Player.Character then -- Ensure the charater has loaded
		Player.CharacterAdded:Wait()
	end	
	local Char = Player.Character
	local Backpack = Player:FindFirstChildOfClass('Backpack')
	
	
	local function getSlotFromTool(tool)
		for i,v in pairs(SlotsFolder:GetChildren()) do
			if v.Value == tool then
				return v
			end
		end
		return false
	end
	
	-- returns every tool the user owns
	local function getAllTools()
		local tools = {}
		for i,v in pairs(Backpack:GetChildren()) do
			if v:IsA('Tool') then
				table.insert(tools,v)
			end
		end
		for i,v in pairs(Char:GetChildren()) do
			if v:IsA('Tool') then
				table.insert(tools,v)
			end
		end
		return tools
	end
	
	-- returns tools not being displayed
	local function getOverflowedTools()
		local tools = {}
		for i,v in pairs(getAllTools()) do
			if not getSlotFromTool(v) then
				table.insert(tools,v)
			end
		end
		return tools
	end
	
	-- Removes tool from the slots folder's records
	local function removeToolFromSlots(tool)
		for i,v in pairs(SlotsFolder:GetChildren()) do
			if v.Value == tool then
				v.Value = nil
				if overflowCollapse then
					for i,v in pairs(getOverflowedTools()) do
						v.Parent = script
						v.Parent = Backpack
					end
				end
				return true
			end
		end
		return false
	end
	
	-- Returns true if tool is assigned to a slot
	local function checkIfToolInSlots(tool)
		for i,v in pairs(SlotsFolder:GetChildren()) do
			if v.Value == tool then
				return true
			end
		end
		return false
	end
	
	-- Adds tool to slots folder's records
	local function addToolToSlots(tool)
		if not tool:IsA('Tool') then -- failsafe
			return nil
		end
		if checkIfToolInSlots(tool) then -- prevents resorting
			return true
		end
		-- If user drops tool, remove from slots folder's records
		tool.AncestryChanged:Connect(function()
			if (tool.Parent ~= Char) and (tool.Parent ~= Backpack) then
				removeToolFromSlots(tool)
			end
		end)
		for i,v in pairs(SlotsFolder:GetChildren()) do
			if v.Value == nil then
				v.Value = tool
				if tool.Parent == Char then
					EquippedVal.Value = v
				end
				return true
			end
		end
		return false
	end
	
	-- Make sure stuff inside backpack gets added to slots folder
	for i,v in pairs(Backpack:GetChildren()) do
		addToolToSlots(v)
	end
	Backpack.ChildAdded:Connect(addToolToSlots)
	
	-- Make sure stuff inside character gets added to slots folder
	for i,v in pairs(Char:GetChildren()) do
		addToolToSlots(v)
	end
	Char.ChildAdded:Connect(addToolToSlots)
	
	-- Equips given tool or unequips everything if given nil
	local function equip(tool)