Once clicked, always sticked (PROBLEM)

I have a problem…

I am making a dragable window (like in Operation Systems) and when I try to grab it, it will stick permanently to your cursor:

But I want it to follow the cusor only when the MouseButton is down.

local down = false
script.Parent.MouseButton1Down:Connect(function()
	down = true
end)
script.Parent.MouseButton1Up:Connect(function()
	down = true
end)
local mouse = game.Players.LocalPlayer:GetMouse()
while true do
	wait()
	if down == true then
		script.Parent.Parent.Position = UDim2.new(0, mouse.X, 0, mouse.Y+75)
	else
	end
end

In this part:

script.Parent.MouseButton1Down:Connect(function()
	down = true
end)
script.Parent.MouseButton1Up:Connect(function()
	down = true
end)

Both of these are setting it to true, try replacing the MouseButton1Up variable change with false:

script.Parent.MouseButton1Down:Connect(function()
	down = true
end)
script.Parent.MouseButton1Up:Connect(function()
	down = false
end)
3 Likes

What @Inconcludable said is correct, but I recommend you change the script a bit and do this instead:

local mouse = game:GetService("Players").LocalPlayer:GetMouse()

local down = false

script.Parent.MouseButton1Down:Connect(function()
	down = true
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then down = false end
end)

while true do
	if down then
		script.Parent.Parent.Position = UDim2.fromOffset(mouse.X, mouse.Y + 75)
	end

	task.wait()
end

The reason why is because sometimes the MouseButton1Up event on Guis doesn’t accurately detect when the player stops holding the mouse button if they do so when the cursor is outside the Gui

Edit: I fixed a bug in my code, now it should work correctly

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.