Problem on drag and drop inventory

How do I detect what slot my mouse is over?!?! For my drag and drop inventory, I have everything made already, but I can’t figure out how to make the slots get placed down.

In short, I have everything made for my inventory, such as the drag part, but the drop part is what I don't know how to make. I can’t really explain further so I have a video that shows my problem.

2 Likes

Try this function to detect if your mouse is inside a frame:

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

local function getMousePositionInFrame(frame): (Vector2, boolean)
	-- Get absolute position of the mouse
	local mousePos = UserInputService:GetMouseLocation()
	-- Get absolute position and size of the frame
	local absPos = frame.AbsolutePosition
	local absSize = frame.AbsoluteSize
	-- Calculate position relative to the frame
	local relativeX = mousePos.X - absPos.X
	local relativeY = mousePos.Y - absPos.Y
	-- Check if mouse is inside the frame
	local isInside = (relativeX >= 0 and relativeX <= absSize.X) and (relativeY >= 0 and relativeY <= absSize.Y)
	return Vector2.new(relativeX, relativeY), isInside
end

The function returns a Vector2 and a boolean. If the boolean is true, it indicates that your mouse is inside the frame.

2 Likes

I implemented your function and made it looped through all of my frames, although it said false for all of them, even the one with my mouse hovering inside the frame I wanted to switch with.

Sorry i forgot to use :GetGuiInset, here is the correct script:

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

local function getMousePositionInFrame(frame): (Vector2, boolean)
	-- Get absolute position of the mouse
	local mousePos = UserInputService:GetMouseLocation() - game.GuiService:GetGuiInset()

	-- Get absolute position and size of the frame
	local absPos = frame.AbsolutePosition
	local absSize = frame.AbsoluteSize

	-- Check if mouse is inside the frame
	local relative = mousePos - absPos
	local isInside = (
		relative.X >= 0 and relative.X <= absSize.X and
			relative.Y >= 0 and relative.Y <= absSize.Y
	)

	return relative, isInside
end

How did you even come up with this?

i forgot but i found this way on this platform

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