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