roblox lua gui drag
Create a new ScreenGui object by using the following code:
lua local gui = Instance.new("ScreenGui") gui.Parent = game.Players.LocalPlayer.PlayerGui
Create a new Frame object as a child of the ScreenGui object:
lua local frame = Instance.new("Frame") frame.Parent = gui
Set the position and size of the Frame object using the properties
Position
andSize
:lua frame.Position = UDim2.new(0, 100, 0, 100) frame.Size = UDim2.new(0, 200, 0, 200)
Enable the Frame object to be draggable by the user by setting the
Draggable
property to true:lua frame.Draggable = true
Add a MouseButton1Down event to the Frame object to handle the dragging functionality: ```lua frame.MouseButton1Down:Connect(function() local mouse = game.Players.LocalPlayer:GetMouse() local startPos = mouse.X local framePos = frame.Position.X.Offset
local function updateDrag(input) local delta = input.Position.X - startPos frame.Position = UDim2.new(0, framePos + delta, 0, frame.Position.Y.Offset) end
local function stopDrag() mouse.Move:Disconnect() mouse.Button1Up:Disconnect() end
mouse.Move:Connect(updateDrag) mouse.Button1Up:Connect(stopDrag) end) ```
Set the
Visible
property of the ScreenGui object to true to make it visible to the player:lua gui.Visible = true
Now you have a draggable GUI element in your Roblox game created using Lua!