Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to make a Button Draggable only on one axis?

Asked by
Discern 1007 Moderation Voter
9 years ago

So I am making a game that requires a TextButton (Can be changed if needed, since there is no text) to be dragged. The problem is, I have no idea how to make it draggable on only one axis. I set it's Draggable property to true, but I can move it on both axes. How can I make it so it can move on one axis?

Would I use a while loop to keep the axis the same, or is there some sort of method? I'm not requesting a script, I just want to know some properties and methods that could accomplish this.

0
You can try creating a function/loop that checks the position of the button every time the position is changed, and then sets its X/Y position to whatever you would like. SlickPwner 534 — 9y

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

It's you're lucky day, this is what I just finished doing! I made it move in intervals, complicating the matter, but it's surprisingly easy(ish) to move a GUI on one axis.

I'd suggest not using the draggable property. I believe it is easier using MouseButton1Down and detecting when and how far the mouse is dragged using the Move event.

local mouse = game.Players.LocalPlayer:GetMouse() -- Need mouse for Move event
local gui = script.Parent -- Location of GuiButton

gui.MouseButton1Down:connect(function()
    local  dragging = true -- Enable dragging
    mouse.Move:connect(function()
        if dragging then
            gui.Position = UDim2.new(0, mouse.X, 0, gui.Position.Y.Offset) -- Set the new position
        end
    end)
    mouse.Button1Up:connect(function()
        dragging = false -- Disable dragging
    end)
    gui.MouseButton1Up:connect(function()
        dragging = false -- Disable dragging
    end)
end)

Only problem I'm having is that once mouse.Move is called it will always be checking to see if the mouse is moving (hense the dragging variable). So if someone knows how to make Button1Up and MouseButton1Up stop mouse.Move from running, I could make the transition from still to moving smoother. This still works, but if you know how to solve this problem leave a comment!

0
Well I don't know how to stop mouse.Move from running, but I just wanted to say that the function "mouse.Button1Up" isn't needed, since your mouse is already on the GUI. The function below would cover that. Discern 1007 — 9y
0
I used mousebutton1down so that you must click on it in order to drag it. BlackJPI 2658 — 9y
Ad

Answer this question