I have an imagebutton inside a frame that when I click and drag it will follow, this works perfectly when it is not parented with the frame. When it’s in the frame the gui will skip and go in the opposite direction of the mouse.
video
local InventoryGui = script.Parent
local InventoryFrame = InventoryGui.Inventory
--local Topbar = InventoryGui.TopToolbar
local Dragging = false
for i,v in pairs(InventoryFrame:GetChildren()) do
if v:IsA("ImageButton") then
v.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
Dragging = true
end
end)
v.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
v.Position = UDim2.new(0, input.Position.X, 0, input.Position.Y)
print(input.Position)
end
end)
v.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
v.Position = UDim2.new(0, input.Position.X, 0, input.Position.Y)
Dragging = false
end
end)
end
end
This may be because you’re not using the “Dragging” variable in a useful way and not setting the UserInputType in InputChanged to Enum.UserInputType.MouseMovement
. The GUIObject.InputChanged
event should also be replaced to it’s UserInputService equivalent to handle mouse movement outside of the GUIObject.
local InventoryGui = script.Parent
local InventoryFrame = InventoryGui.Inventory
–local Topbar = InventoryGui.TopToolbar
local Dragging = false
for i,v in pairs(InventoryFrame:GetChildren()) do
if v:IsA("ImageButton") then
v.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
Dragging = true
end
end)
game:GetService("UserInputService").InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement and Dragging then
v.Position = UDim2.new(0, input.Position.X, 0, input.Position.Y)
print(input.Position)
end
end)
v.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
v.Position = UDim2.new(0, input.Position.X, 0, input.Position.Y)
Dragging = false
end
end)
end
end
Sorry for the bad formatting.
It’s moving all the images instead of one?
Whoops, I forgot you had more than one image. Updated code:
local InventoryGui = script.Parent
local InventoryFrame = InventoryGui.Inventory
–local Topbar = InventoryGui.TopToolbar
local Dragging = false
local SelectedDraggable
for i,v in pairs(InventoryFrame:GetChildren()) do
if v:IsA("ImageButton") then
v.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
SelectedDraggable = v
Dragging = true
end
end)
game:GetService("UserInputService").InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement and Dragging and SelectedDraggable then
SelectedDraggable.Position = UDim2.new(0, input.Position.X, 0, input.Position.Y)
print(input.Position)
end
end)
v.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
v.Position = UDim2.new(0, input.Position.X, 0, input.Position.Y)
Dragging = false
SelectedDraggable = nil
end
end)
end
end
What I did here is that I added a variable called “SelectedDraggable”. It’s set on the InputBegan event. You wanted only one image to move, right? So, the InputChanged event instead checks for that new variable to be declared, and if it was, move the selected draggable to the mouse’s position.
A solution probably better than this
2 Likes
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
YourImageButton.MouseButton1Down:Connect(function()
YourImageButton.Position = UDim.fromOffset(mouse.X, mouse.Y)
end)
1 Like