Attaching / Deattaching gui from mouse icon

I want my gui to attach to the mouse icon when the player holds down on the gui. I want my gui to deattach when the player lifts their mouse button up.

The gui attaches to the mouse icon, but it does not deattach.

I have tried adding debounce (did not work). I’ve also used print(), and the code works, it’s just that the gui does not deattach.

MY CODE

local mouse = game.Players.LocalPlayer:GetMouse()
local isattached = false

script.Parent.MouseButton1Down:Connect(function()
	if isattached == false then isattached = true
		print("nowAttached")
		game:GetService("RunService").RenderStepped:Connect(function()
			script.Parent.Position = UDim2.new(0,mouse.X,0,mouse.Y)
		end)
	end
end)

script.Parent.MouseButton1Up:Connect(function()
	if isattached == true then
		isattached = false
		print("Now unattached")
		script.Parent.Position = UDim2.new(0.5,0,0.5,0)
	end
end)

The RenderStepped function still runs even after releasing your mouse. Try disconnecting it.

local mouse = game.Players.LocalPlayer:GetMouse()
local isattached = false
local connection

script.Parent.MouseButton1Down:Connect(function()
	if isattached == false then isattached = true
		print("nowAttached")
		connection = game:GetService("RunService").RenderStepped:Connect(function()
			script.Parent.Position = UDim2.new(0,mouse.X,0,mouse.Y)
		end)
	end
end)

script.Parent.MouseButton1Up:Connect(function()
	if isattached == true then
		isattached = false
		connection:Disconnect()
		print("Now unattached")
		script.Parent.Position = UDim2.new(0.5,0,0.5,0)
	end
end)

I tried this and it did not work. I greatly appreciate your effort to help though!