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

Help with GUI Dragging?

Asked by 8 years ago

I'm trying to create a GUI that when you click it, it locks on to your mouse.

This is the simple script:

script.Parent.MouseButton1Down:connect(function(x,y)
    local startPos = script.Parent.Position
        local conn = script.Parent.MouseMoved:connect(function(X,Y)
            script.Parent.Position = startPos + UDim2.new(0,X-x,0,Y-y)
        end)
end)

For the most part it all works out, but when I test it this happens.

The GUI stops locking on when the mouse goes too fast. I've been stumped on this for a while, any help or suggestions?

0
You could try using a loop to check to see if the mouse has moved, instead of using an event. But it would be harder on the server. TheDeadlyPanther 2460 — 8y

2 answers

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

You could try adding in a loop which runs when the mouse is down, and a mouseup event to break the loop.

script.Parent.MouseButton1Down:connect(function(x, y)
    clicked = true
    local startPos = script.Parent.Position
    local mouse = player:GetMouse()
    while clicked do
        script.Parent.Position = startPos + UDim2.new(0 ,mouse.X-x, 0, mouse.Y-y)
        wait() -- Very important edit I forgot to throw in.. Woops
    end
end)

script.Parent.MouseButton1Up:connect(function()
    clicked = false
end)
Ad
Log in to vote
0
Answered by 8 years ago

Pyrodon, you helped me get my final result. Thanks!

It ended up looking like this:

clicked = false

script.Parent.MouseButton1Down:connect(function(x, y)
    if clicked == false then
        clicked = true
        local startPos = script.Parent.Position
        local mouse = game.Players.LocalPlayer:GetMouse()

        while clicked do
            wait()
            script.Parent.Position = startPos + UDim2.new(0,mouse.X-x,0,mouse.Y-y+36)
        end
    end
end)

Answer this question