Inventory System

Hello people from this earth,

I was thinking about a new inventory system for Roblox. I had seen this inventory on a FiveM server and thought it would be a cool idea to include it in roblox.

I copied the GUI in Roblox and wrote some here and there so that the system is already working. I have now come to the point that I would like to drag the item to the hand tool so that the user can use the item. I’ve looked around the DevForum a bit but couldn’t find anything that really helps me.

Good to know: The items are in a GridLayout

summary: the item must be able to be dragged to the hand where it says use

sorry for my bad english, i’m dutch. I hope I was clear and you can help me


1 Like

What I did for my inventory system is listen to when the user holds down the left click button, check what item it is hovering over, and then make a copy (clone the UI) of the item and constantly position it at the mouse’s position. Once the player releases the mouse button, I check what it is hovering over, and if it can be placed there, I “move” the item there. This should work for your case as well.

Sounds like it should work, I’ll try it. I’ll let you know if it works for me

Yow, I tested and tried a bit and couldn’t really get it to work. Would you happen to share a piece of code? So that I can be on the right track

Apologies for the late reply. I am not currently home, but once I get home, I’ll be sure to send you a sample of how I did it.

Here is what I used in my inventory system:

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if GameProcessed then return end
	
	if not MouseDebounce and not HoldingItem and Input.UserInputType == Enum.UserInputType.MouseButton1 then
		MouseDebounce = true
		
		local MousePosition = UserInputService:GetMouseLocation() - Vector2.new(0, 36)
		local Item = nil
		
		for _, Region in ipairs({NewRegion, GroundRegion}) do
			local Slot = Region:GetSlotFromPosition(MousePosition) -- Gets the slot in which the mouse is currently hovering over (this uses certain calculations to achieve it)
			if Slot then
				local SlotItem = Slot.OccupiedBy
				if SlotItem then
					Item = SlotItem
					break
				end
			end
		end
		
		if not Item then MouseDebounce = false; return end
		
		local ItemClone = Item.ItemFrame:Clone()
		ItemClone.AnchorPoint = Vector2.new(0.5, 0.5)
		ItemClone.Position = UDim2.new(0, MousePosition.X, 0, MousePosition.Y)
		ItemClone.Parent = script.Parent
		
		HoldingItemObject = Item
		HoldingItem = ItemClone
		
		MouseDebounce = false
	end
end)

UserInputService.InputChanged:Connect(function(Input, GameProcessed)
	if HoldingItemObject and Input.UserInputType == Enum.UserInputType.MouseMovement then
		local MousePosition = UserInputService:GetMouseLocation() - Vector2.new(0, 36)
		HoldingItem.Position = UDim2.new(0, MousePosition.X, 0, MousePosition.Y)
	end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcessed)
	if not MouseDebounce and HoldingItemObject and HoldingItem and Input.UserInputType == Enum.UserInputType.MouseButton1 then
		MouseDebounce = true
		
		local MousePosition = UserInputService:GetMouseLocation() - Vector2.new(0, 36)
		local SlotPosition = nil
		
		for _, Region in ipairs({NewRegion, GroundRegion}) do
			local Slot = Region:GetRegionFromPosition(MousePosition, HoldingItemObject.Size)
			if Slot and not Region:GetFirstOccupiedInRegion(Slot.Position, HoldingItemObject.Size) then
				SlotPosition = Slot
				break
			end
		end
		
		if SlotPosition then
	        -- Unnecessary to show this, but it basically marks the slots as occupied
		end
		
		HoldingItem:Destroy()
		HoldingItem = nil
		HoldingItemObject = nil
		
		MouseDebounce = false
	end
end)

As for the elephant in the room, the GetSlotFromPosition() function, I used position calculations to find it. You could have a MouseEnter and MouseLeave event listener for each thing to detect when the mouse enters and leaves, or implement it mathematically like me. But, for as long as you have the item it is hovering over (and clicked over), then the rest should be easy, you just need to check if it is hovering over the “Use Item” UI. If you don’t know how to do that, then you might find useful a topic that I created a little while ago about accurately checking if the mouse is within a 2D region.

Hopefully this helped you.


yow this is what i have now. Only when I drag the Frames are still a bit weird.

The problem might be because the position of the dragged frame is relative (as in, (0, 10, 0, 10) within a UI that is in the center is not the same as if it was a frame inside the ScreenGui itself). A simple fix is to parent the frame to the ScreenGui itself or some frame that has a size of (1, 0, 1, 0) (basically, the screen size).

I solved it in a cheap way hahaha. UDim2.new(-0.2, MousePosition.X, 0, MousePosition.Y)

I mean, if that works, then it is good. For as long as the measurements counter the frame measurements, (so if your scale is 0.2 and 0.4, -0.2 and -0.4 would counter it to basically revert it to 0) it is completely fine. If that solved your issue, then make sure to mark a reply as a solution for future reference.

1 Like

Thank you for helping me, I really appreciate it

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.