Draggable inventory objects

Need help making the draggable objects move around in the inventory, but with code below it doesn’t move as intended.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Backpack = Player.Backpack
local Mouse = Player:GetMouse()

local PlayerGui = Player.PlayerGui

local scriptStartTick = tick()
local Gui = script.Parent
local addedTools = {}

local Configuration = script.Configuration

local isDraggingItem = false
local isItemEquipped = false
local dragging = false
local dragStart = nil
local startPos = nil
local draggedItem = nil
local equippedItem = nil

local Template = script.Template

local Keys = {
	[Enum.KeyCode.One] = 1,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 5,
	[Enum.KeyCode.Six] = 6,
	[Enum.KeyCode.Seven] = 7,
	[Enum.KeyCode.Eight] = 8,
	[Enum.KeyCode.Nine] = 9,
	[Enum.KeyCode.Zero] = 10,
}

StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local Root = Gui.Root
local BackpackFrame = Root.BackpackFrame
local Inventory = Root.Inventory
local DraggingFrame = Root.DraggingFrame
local Main = Inventory.Main
local List = Main.List

function AddSlot(Tool)
	addedTools = {}  -- Clear the Tools table

	for i, v in ipairs(Backpack:GetChildren()) do
		if v:IsA("Tool") then
			table.insert(addedTools, v)

			local SlotClone = Template:Clone()
			SlotClone.Name = v.Name
			SlotClone.Parent = List
			SlotClone.Button.Text = v.Name
			SlotClone.Visible = true

			SlotClone.Button.MouseButton1Click:Connect(function()
				EquipItem(v)
			end)
		end
	end
end

function EquipItem(Tool: Tool)
	if Tool then
		Humanoid:EquipTool(Tool)
	else
		Humanoid:UnequipTools()
	end
end

Backpack.ChildAdded:Connect(function(child)
	if child:IsA("Tool") then
		AddSlot(child)
	end
end)

Character.ChildAdded:Connect(function(child)
	if child:IsA("Tool") then
		AddSlot(child)
	end
end)

for _, tool in ipairs(Backpack:GetChildren()) do
	AddSlot(tool)
end

UserInputService.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == Enum.KeyCode.Backquote then
		Main.Visible = not Main.Visible
	end
end)

Template.InputBegan:Connect(function(input: InputObject)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		dragging = true
		dragStart = Vector2.new(input.Position.X, input.Position.Y)
		startPos = Vector2.new(input.Position.X, input.Position.Y)
		draggedItem = Template:Clone()
		draggedItem.Parent = DraggingFrame
		draggedItem.Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)
		draggedItem.Visible = true
		
		DraggingFrame.Visible = true
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement and dragging then
		local delta = input.Position - dragStart
		draggedItem.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and dragging then
		dragging = false
		draggedItem:Destroy()
		DraggingFrame.Visible = false
	end
end)