Inventory system, Swap slots

Hi, im first time trying to make inventory system.
I struggling how to make function to swap slots.
Any ideas how to make it?
Video how inventory works

Script inside ViewportFrame

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Main = script.Parent
local UiDrag = Main.UiDrag
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid",5)
local Mouse = Player:GetMouse()

local MainInventoryFrame = Main.Parent.Parent.Parent.Parent
local ParentFrame = Main.Parent
local IsMovingSomething = MainInventoryFrame.Parent.Info:WaitForChild("IsMovingSomething")
print(MainInventoryFrame)

local IsMovingrn = false

UiDrag.DragStart:Connect(function()
	IsMovingrn = true
	IsMovingSomething.Value = true
end)


UiDrag.DragEnd:Connect(function()
	IsMovingrn = false
	IsMovingSomething.Value = false
	Main.Parent = ParentFrame
	Main.Position = UDim2.new(0, 0, 0, 0) 
end)

Main.MouseEnter:Connect(function()
	if IsMovingSomething.Value == false then
	local AbsolutePosition = Main.AbsolutePosition
	Main.Parent = MainInventoryFrame
	local newX = AbsolutePosition.X - MainInventoryFrame.AbsolutePosition.X
	local newY = AbsolutePosition.Y - MainInventoryFrame.AbsolutePosition.Y
	Main.Position = UDim2.new(0, newX, 0, newY)
	end
end)

Main.MouseLeave:Connect(function()
	if IsMovingrn == false then
	Main.Parent = ParentFrame
	Main.Position = UDim2.new(0, 0, 0, 0) 
	end
end)

If I understand this, you want to move an item into the empty slot. What you’d do is find what slot the mouse drag ends at, that will be the target. then you can take the GuiElement and re-parent it to the new slot frame.

Yea, that’s the problem, i dont know how to find target when mouse drag end

UIDragDetector | Documentation - Roblox Creator Hub the event has a vector2 parameter for the position. So you have to somehow calculate what frame that’d be.

since each slot is a rectangle you can use the absolutePosition and the absoluteSize to get a rectangular bound of the box and then check if player:GetMouse().X and mouse.Y are inside