Change position of UI Object (with the mouse)

Hello,

I am creating a plugin but I would like to move an image when I am clicking on it with my left click but it’s not working. I have nothing in the output…
frame is the scene where the image can be.
image is the target to move, I use an ObjectValue to do a reference to the image.

local mouse = plugin:GetMouse()

local frame = script.Parent
local image = script.Value.Value

local isDragging = false
local startPos
local frameSize = frame.AbsoluteSize
local imageOffset = Vector2.new(0, 0)

local function updateImagePosition(mousePosition)
	if isDragging then
		local newPosition = UDim2.new(0, mousePosition.X - startPos.X + imageOffset.X, 0, mousePosition.Y - startPos.Y + imageOffset.Y)
		local minX = 0
		local minY = 0
		local maxX = frameSize.X - image.Size.X.Offset
		local maxY = frameSize.Y - image.Size.Y.Offset
		newPosition = UDim2.new(
			0, math.clamp(newPosition.X.Offset, minX, maxX),
			0, math.clamp(newPosition.Y.Offset, minY, maxY)
		)
		image.Position = newPosition
	end
end

image.InputBegan:Connect(function(input) 
	if input.UserInputType == Enum.UserInputType.MouseButton1 then 
		isDragging = true
		startPos = Vector2.new(mouse.X, mouse.Y)
		imageOffset = Vector2.new(image.Position.X.Offset, image.Position.Y.Offset)
	end
	print(1)
end)

image.InputEnded:Connect(function(input) 
	if input.UserInputType == Enum.UserInputType.MouseButton1 then 
		isDragging = false
	end
	print(2)
end)

image.InputChanged:Connect(function(input) 
	print(3)
	if isDragging and input.UserInputType == Enum.UserInputType.MouseMovement then
		updateImagePosition(input.Position)
	end
end)
1 Like