Off we start I want to mention that I know there are already plenty of discussions on this problem but I’ve gone and read most of them and none seem to work for my case. As title already suggests - I am trying to set the frame position to the exact position of mouse when player clicks (for drag and drop system)
All those frames are in Inventory > InventoryFrame > Items but get moved to Inventory > InventoryFrame on click because I can’t move them if they are in Inventory > InventoryFrame > Items because of UIGridLayout.
for _, item in ipairs(items) do
local itemFrame = itemTemplate:Clone()
itemFrame.Parent = script.Parent.InventoryFrame[path]
itemFrame.ItemName.Text = item.name
itemFrame.ItemRarity.Text = item.rarity
itemFrame.ItemRarityColor.BackgroundColor3 = item.rarityColor
itemFrame.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
draggable = true
while wait() do
if draggable then
temFrame.Parent = script.Parent.InventoryFrame
itemFrame.Position = UDim2.new(0, mouse.X, 0, mouse.Y) -- HERE
else
break
end
end
end
end)
itemFrame.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
draggable = false
end
end)
end
The current way of assigning the position work like this →
local trueMousePosition = UserInputService:GetMouseLocation() - GuiService:GetGuiInset()
trueMousePosition.X, trueMousePosition.Y -- something like this I think
Taken from my viewport mouse raycasting where I found out GuiInset offsets mouse location considerably.
for _, item in ipairs(items) do
local itemFrame = itemTemplate:Clone()
itemFrame.Parent = script.Parent.InventoryFrame[path]
itemFrame.ItemName.Text = item.name
itemFrame.ItemRarity.Text = item.rarity
itemFrame.ItemRarityColor.BackgroundColor3 = item.rarityColor
itemFrame.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
draggable = true
while wait() do
if draggable then
itemFrame.Parent = script.Parent.InventoryFrame
itemFrame.Position = UDim2.new(0, mouse.X, 0, mouse.Y) -- HERE
else
break
end
end
end
end)
itemFrame.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
draggable = false
end
end)
end