Scripting helping UDim2

The Local script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.MouseButton1Click:Connect(function()
if mouse.Target then
script.Parent.Position = UDim2.new(0,mouse.X,0,mouse.Y)
end
end)

My problem: When i click the button goes off the screen.

Seems like you’re using the Offset property, couldn’t you use the Size instead?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.MouseButton1Click:Connect(function()
    if mouse.Target then
        script.Parent.Position = UDim2.new(mouse.X, 0, mouse.Y, 0)
    end
end)
2 Likes

Definitely not, that would make it go ScreenWidth * MouseX pixels across, which for reference is about this number with the mouse at the middle on a 1920 monitor: 1843200

1 Like

If that object is in a frame or another GuiObject, make sure you account for the parents position as well

local ParentPos = script.Parent.Parent.AbsolutePosition
UDim2.new(0, mouse.X - ParentPos.X, 0, mouse.Y - ParentPos.Y)`