I am attempting to make a backpack with movable items but for some reason when I use the function .fromOffset()
for position UI elements it always returns 0 on the scale element no matter what the position of the mouse is.
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Mouse = game.Players.LocalPlayer:GetMouse()
local UI = script.Parent:WaitForChild("InventoryGrid")
local CurrentUIElement
local TargetLayoutOrder = 1
local screenSize = workspace.CurrentCamera.ViewportSize
local function onMouseUpdate()
if CurrentUIElement then
local mouseScale = UDim2.fromOffset(Mouse.X, Mouse.Y)
CurrentUIElement.Position = mouseScale
for _,v in ipairs(UI.Main.Inventory:GetChildren()) do
if v:IsA("Frame") then
local frameScale = UDim2.fromOffset(v.AbsolutePosition.X, v.AbsolutePosition.Y)
print(mouseScale.X)
print(frameScale.X)
if mouseScale.X.Scale > frameScale.X.Scale and mouseScale.Y.Scale > frameScale.Scale.Y then
UI.Main.TextLabel.Text = v.WeaponName.Text
TargetLayoutOrder = v.LayoutOrder + 1
end
end
end
end
end
for _,v in ipairs(UI.Main.Inventory:GetChildren()) do
if v:IsA("Frame") then
v.TextButton.MouseButton1Down:Connect(function()
RunService:BindToRenderStep("mouseUpdate", 1, onMouseUpdate)
local FrameHolder = v:Clone()
FrameHolder.Parent = UI
FrameHolder.Size = UDim2.fromOffset(UI.Main.Inventory.UIGridLayout.AbsoluteCellSize.X, UI.Main.Inventory.UIGridLayout.AbsoluteCellSize.Y)
FrameHolder.AnchorPoint = Vector2.new(0.5,0)
CurrentUIElement = FrameHolder
v.BackgroundTransparency = 1
v.WeaponName.TextTransparency = 1
local connection
connection = UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
connection:Disconnect()
RunService:UnbindFromRenderStep("mouseUpdate")
v.LayoutOrder = TargetLayoutOrder
v.Parent = UI.Main.Inventory
v.BackgroundTransparency = 0
v.WeaponName.TextTransparency = 0
for _,v in ipairs(UI.Main.Inventory:GetChildren()) do
if v:IsA("Frame") then
if v.LayoutOrder >= TargetLayoutOrder then
v.LayoutOrder = v.LayoutOrder + 1
end
end
end
FrameHolder:Destroy()
end
end)
end)
end
end