Custom Hotbar with Dragging items

I need make a hotbar, like in minecraft, i need to drag items from inventory to hotbar, how i do it?(sorry, for small details)

For the dragging part, here’s the basic structure for it.

You’ll need to modify the code below so that it clones the slot being dragged when the player holds left click and positions the cloned slot to the mouse’ position and when the hold click ends, you’ll need to destroy the cloned slot.

When the hold click ends, You’ll need to check if the player’s mouse is hovering over a slot, after that, you check if that slot is empty.

How can you detect when the player holds left click?
Well, you’ll need UserInputService to do so.

local UserInputService = game:GetService("UserInputService")

local gui = script.Parent

local dragging
local dragInput
local dragStart
local startPos

local function update(input)
	local delta = input.Position - dragStart
	gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
end

gui.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		dragging = true
		dragStart = input.Position
		startPos = gui.Position
		
		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				dragging = false
			end
		end)
	end
end)

gui.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		dragInput = input
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if input == dragInput and dragging then
		update(input)
	end
end)

Source

1 Like