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?
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)