Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with GUI Dragging?

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?

1 comment

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.

2 answers

✓ Accepted answer

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)

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.