Problem with item dragging inventory

I am working on an item dragging inventory and i have came across a problem where things get picked and placed at wrong places and i don’t know what i did wrong…

Heres an video explaining my issue:
robloxapp-20210216-2215344.wmv (489,9,KB)

and Heres my code:

local RunService=game:GetService("RunService")
local CurrentObject=nil
local CurrentObjectsTable={}
local CurrentParent=nil
local Holding=false
local Dragging=false
local Player=game:GetService("Players").LocalPlayer
local StarterGui=Player:WaitForChild("PlayerGui")
local Inventory=script.Parent
local Backpack=Inventory:WaitForChild("Backpack")
local Items=Backpack:WaitForChild("Items")
local HotSlots=Backpack:WaitForChild("HotSlots")
local ReplicatedStorage=game:GetService("ReplicatedStorage")
local Inventories=ReplicatedStorage:WaitForChild("Inventories") 
local PInventory=Inventories:WaitForChild(Player.Name.."_INV_"..Player.UserId)
local ContextActionService=game:GetService("ContextActionService")
Mouse=Player:GetMouse()



Mouse.Button1Down:Connect(function()
	if CurrentParent~= nil and CurrentObject~= nil then
		CurrentObject.Parent=Inventory
		print(CurrentObject)
		Dragging=true
	end
end)
Mouse.Button1Up:Connect(function()

	Dragging=false

	local GUIs = StarterGui:GetGuiObjectsAtPosition(Mouse.X, Mouse.Y-36)
	for i=1,#GUIs do 
		local CurrentSelection=GUIs[i]
		if CurrentSelection:FindFirstChild("ItemHolder") and CurrentObject~=nil then
			local t=#CurrentSelection.ItemHolder:GetChildren()
			if t>=2 then 
				return 
			else 
				CurrentObject.Parent=CurrentSelection.ItemHolder
			end 
		end
	end 
end)
Mouse.Move:Connect(function()
	if Dragging then return end
	local GUIs = StarterGui:GetGuiObjectsAtPosition(Mouse.X, Mouse.Y-36)
	CurrentObjectsTable=GUIs
	for i=1,#CurrentObjectsTable do 
		local CurrentSelection=CurrentObjectsTable[i]
		if CurrentSelection.Parent==Items and not Dragging then
			CurrentObject=CurrentSelection
			CurrentParent=Items
			--print(CurrentObject)
			--elseif Dragging and CurrentSelection:IsDescendantOf() then 

		end
	end
end)
RunService.RenderStepped:Connect(function()
	if Dragging then 
		CurrentObject.Position=UDim2.new(0,Mouse.X,0,Mouse.Y)
	end
end)

This means “put the top left corner of CurrentObject at the mouse’s position”.

You’ll need to save the initial offset when you first click, and add it to the position when you update it:

-- ...
local offsetX, offsetY = 0, 0

Mouse.Button1Down:Connect(function()
	if CurrentParent~= nil and CurrentObject~= nil then
		CurrentObject.Parent=Inventory
		print(CurrentObject)
		Dragging=true

		-- save first offset
		offsetX = CurrentParent.AbsolutePosition.X - Mouse.X
		offsetY = CurrentParent.AbsolutePosition.Y - Mouse.Y
	end
end)

-- ...


RunService.RenderStepped:Connect(function()
	if Dragging then 
		-- and apply the offset when you move it
		CurrentObject.Position=UDim2.new(0,Mouse.X + offsetX,0,Mouse.Y + offetY)
	end
end)
1 Like

Okay,I’m very thankful for that,it infact solved the moving offset problem but i still have another problem,i have to click slightly above the objects to pick them up and release slightly under the slots to place them,any idea on what could be happening?

Edit:
And if the mouse is over the scroll gui i can’t drop the item