Drop function for my drag and drop inventory doesn't function as intended

I'm making a drag and drop inventory system from scratch, and everything works besides the drop system.

It usually works on pc, but it’s most definitely not full proof. The main problem is that it doesn’t work on mobile.

The way my drop function works is that it gets all of the guiobjects at the mouse’s position, and stores them in a table. Then I use a for loop using the table’s data to see if there is any objects that I can interact with. The loop always breaks after the first instance, so that the itemSlot doesn’t move to another parent that it’s not supposed to go to.

I know there is huge problems with the way I programmed this, since there are many factors that has to be exact for this function to function properly. Such as the first instance in the table is the target that the player was aiming for, or how other objects in the gui can mess up the table/guiObjectsAtPosition.

I want to make a different way of handling this, but I don’t how I could achieve it.

function inventory:dragEnded(itemSlot)
	self.Dragging = false
	self.DragRequest = false

	local mousePos = UserInputService:GetMouseLocation() - GuiService:GetGuiInset()

	local guiObjectsAtPosition = playerGui:GetGuiObjectsAtPosition(mousePos.X, mousePos.Y)
	
	for i, object in guiObjectsAtPosition do
		if object == itemSlot then 
			table.remove(guiObjectsAtPosition, i)
		end
	end

	for i, object in guiObjectsAtPosition do

		if CollectionService:HasTag(object, "Slot") then
			if inventory:slotInHotbar(object) then
				if inventory:slotTakened(object) then
					object:FindFirstChildWhichIsA("TextButton").Parent = self.SlotBeingDragged
					object:SetAttribute("Takened",true)
					itemSlot.Parent = object
					break
				else
					itemSlot.Parent = object
					object:SetAttribute("Takened", true)
					if self.SlotBeingDragged ~= object and not inventory:slotInHotbar(self.SlotBeingDragged) then
						Debris:AddItem(self.SlotBeingDragged, 0)
					end

					break
				end
			end
		elseif object == toolHolder and self.SlotBeingDragged.Parent ~= toolHolder then
			local newSlotHolder = self:makeHolderSlot()
			newSlotHolder.Parent = toolHolder
			itemSlot.Parent = newSlotHolder
			break
		else
			itemSlot.Parent = self.SlotBeingDragged
			break
		end
	end
	
	if #guiObjectsAtPosition == 0 then
		itemSlot.Parent = self.SlotBeingDragged
	end



	itemSlot.Position = UDim2.new(0,0,0,0)
	--itemSlot.Parent = self.SlotBeingDragged

	self.SlotBeingDragged = nil
	self:updateInventory()
end

I’d suggest looking into UserInputService events related to touches on a touch enabled screen, this should help:

I’m restarting the drop function as of right now, and I added a new variable called self.HoveringToolFrame, which just detects what frame my mouse is over.

I don’t know where to go from here so maybe you can help me.

function inventory:dragEnded(itemSlot)
	if self.HoveringToolFrame and self.HoveringToolFrame ~= self.FrameBeingDragged then
		
	end
	
	itemSlot.Parent = self.FrameBeingDragged
	itemSlot.Position = UDim2.new(0,0,0,0)
	
	self.FrameBeingDragged = nil
	self.Dragging = false
	self.DragRequest = false
end

I fixed this by myself. Turns out that I can just use GetGuiObjectsAtPosition to get a certain slot.