My backpack system uh, kinda broke D:

Hi guys, I have a module that handles backpack, since I wanted a new UI design, it works fine but;


(check the numbers of the backpack) :smiley:

Module;

local Frame = script.Parent.Parent.Backpack
local Player = game:GetService("Players").LocalPlayer

local Inventory = {}
local olde = nil

local function Clean(x)
	local a = x
	for i,v in pairs(a) do
		if (not v:IsA("Tool")) then
			table.remove(a,i)
		end
	end
	return a
end

return function()
	if (olde) then
		olde:Destroy(
		)
		olde = nil		
	end
	local EquipEvent = Instance.new("BindableEvent")
	olde = EquipEvent
	local Tools = Player.Backpack:GetChildren()
	if (Player.Character:FindFirstChildWhichIsA("Tool")) then
		for i,v in pairs(Player.Character:GetChildren()) do
			if (v:IsA("Tool")) then
				table.insert(Tools,v)
			end
		end
	end
	Tools = Clean(Tools)
	for i,v in pairs(Frame.CurrentItems:GetChildren()) do
		if (not Tools[v.Name] and v:IsA("Frame")) then
			v:Destroy()
		end
	end
	for i,v in pairs(Tools) do
		if (not Frame.CurrentItems:FindFirstChild(v.Name)) then
			local Template = script.Glock:Clone()
			Template.Name = v.Name
			Template.Parent = Frame.CurrentItems 
			Template.index.Text = i
			Template.name.Text = v.Name
			Template.TextButton.MouseButton1Click:Connect(function()
				script.Parent.Events.BackpackEquip:Fire(Template)
			end)
		end
	end
end

What I do in local script;

local ModuleForBackpack = require(script.BackpackHandler)

Player.Backpack.ChildAdded:Connect(function()
	ModuleForBackpack()
end)
Player.Backpack.ChildRemoved:Connect(function()
	ModuleForBackpack()
end)