Drag UI SUPPORTS MOBILE
Key RequiredUniversal

Drag UI SUPPORTS MOBILE

Universal Scriptareyoumental110areyoumental110 9.3K views 144.1K executesUpdated Nov 22, 2024
5.0/ 5(1 votes)
10100% Positive
Lua Script•131 lines (4922 chars)
local uis = game:GetService("UserInputService")
local localplr = game.Players.LocalPlayer
local mouse = localplr:GetMouse()
local dragging = {}
function dragtopos(fpos,spos,pos,frame,bounds,ar)
    local finalpos = UDim2.new(
        frame.Position.X.Scale, --spos.X.Scale+(fpos.X.Scale-pos.X.Scale),
        spos.X.Offset-(fpos.X.Offset-pos.X),
        frame.Position.Y.Scale, --spos.Y.Scale+(fpos.Y.Scale-pos.Y.Scale),
        spos.Y.Offset-(fpos.Y.Offset-pos.Y)
    )
    if bounds ~= nil then
        local UI = Instance.new("ScreenGui")
        UI.ScreenInsets = Enum.ScreenInsets.None 
        UI.Parent = game.CoreGui
        local screensize = UI.AbsoluteSize
        UI.Enabled = true
        local replframe = frame:Clone()
        replframe.Parent = UI
        replframe.Position = finalpos
        local absp = replframe.AbsolutePosition
        local abss = replframe.AbsoluteSize
        --[[
        replframe.Position = UDim2.new(0,0,0,0)
        local abspl = replframe.AbsolutePosition.X-replframe.AbsoluteSize.X
        local abspt = replframe.AbsolutePosition.Y+replframe.AbsoluteSize.Y
        replframe.Position = UDim2.new(1,0,1,0)
        local abspr = replframe.AbsolutePosition.X
        local abspb = screensize.Y-replframe.AbsolutePosition.Y
        local left = absp.X-abss.X < abspl
        local right = absp.X+abss.X > abspr
        local bottom = absp.Y+abss.Y > abspb
        local top = absp.Y+abss.Y < abspt
        ]]
        finalpos = UDim2.new(
            finalpos.X.Scale,
            math.clamp(
                finalpos.X.Offset,
                -finalpos.X.Scale*screensize.X,
                -finalpos.X.Scale*screensize.X+(screensize.X-abss.X)
            ),
            finalpos.Y.Scale,
            math.clamp(
                finalpos.Y.Offset,
                -finalpos.Y.Scale*screensize.Y,
                (-finalpos.Y.Scale*screensize.Y)+(screensize.Y-abss.Y) -- doesnt actually do the correct position but i gave up
            )
        )
        --[[
        print(left,right,bottom,top)
        if left then
            finalpos = UDim2.new(
                finalpos.X.Scale,
                -finalpos.X.Scale*screensize.X,
                finalpos.Y.Scale,
                finalpos.Y.Offset
            )
        elseif right then
            finalpos = UDim2.new(
                finalpos.X.Scale,
                (-finalpos.X.Scale*screensize.X)+(screensize.X-abss.X),
                finalpos.Y.Scale,
                finalpos.Y.Offset
            )
        end 
        if top then
            finalpos = UDim2.new(
                finalpos.X.Scale,
                finalpos.X.Offset,
                finalpos.Y.Scale,
                -finalpos.Y.Scale*screensize.Y
            )
        elseif bottom then
            finalpos = UDim2.new(
                finalpos.X.Scale,
                finalpos.X.Offset,
                finalpos.Y.Scale,
                (-finalpos.Y.Scale*screensize.Y)+(screensize.Y-abss.Y)
            )
        end
        ]]
        UI:Destroy()
    end
    frame.Position = finalpos
end
local drag = function(frame,bounds,chf)
    local frames = {}
    if frame:IsA("ScreenGui") then
        for i,v in pairs(frame:GetChildren()) do
            if chf ~= nil and (v:IsA("Frame") or v:IsA("ScrollingFrame")) then
                table.insert(frames,v)
            elseif not chf then
                table.insert(frames,v)
            end
        end
    else
        table.insert(frames,frame)
    end
    for i,frame in pairs(frames) do
        frame.Active = true -- uhh
        local w,h = frame.AbsoluteSize.X,frame.AbsoluteSize.Y
        local ar = w/h
        frame.InputBegan:Connect(function(input)
            if (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) and not dragging[input] then
                dragging[input] = frame
                local firstpos = UDim2.new(0,mouse.X,0,mouse.Y)
                local startpos = frame.Position
                local move1 = uis.TouchMoved:Connect(function(input2)
                    if input == input2 then
                        dragtopos(firstpos,startpos,input2.Position,frame,bounds or nil,ar)
                    end
                end)
                local move2 = uis.InputChanged:Connect(function(input2)
                    if input2.UserInputType == Enum.UserInputType.MouseMovement then
                        dragtopos(firstpos,startpos,input2.Position,frame,bounds or nil,ar)
                    end
                end)
                repeat task.wait() until dragging[input] == nil
                move1:Disconnect()
                move2:Disconnect()
            end
        end)
        uis.InputEnded:Connect(function(input)
            dragging[input] = nil
        end)
        uis.InputBegan:Connect(function(input)
            dragging[input] = true
        end)
    end
end
return drag

Features & Information

allows GUI's to be easily dragged. TO USE: simply call the loadstring as a variable like this (the script returns the drag function) local drag = loadstring(game:HttpGet("https://rawscripts.net/raw/Universal-Script-Drag-UI-SUPPORTS-MOBILE-22790"))() The drag function has 1 required argument and 2 other optional ones. drag(FrameOrScreenGUI,Bounds,CheckForFrame) FrameOrScreenGUI is the frame/screengui that you want to drag Bounds makes it so your frame cant get out of the screen (cant go out of it) CheckForFrame is for checking if the frame is a frame before dragging it (for screengui's) If you want to support me, go through this linkvertise https://link-target.net/1262127/support-me
#Drag#GUI#Frame#Draggable

Community Comments (0)

No comments yet for this script.

More Scripts for Universal Scripts