How do i make a GUI be draggable or not draggable according to Bool Values?

I am making a GUI and have trouble from stopping the gui from being able to be dragged around the screen. The script below shows when TouchPart1 is touched a boolvalue becomes true triggering the gui to be able to be dragged. When TouchPart2 is touched it makes the boolvalue become false however it doesnt stop the gui from being able to be dragged. So my question is how do i make a GUI be draggable or not draggable according to Bool Values?

The dragging code was not written by me. I found it when searching for an alternative for the draggable property of guis which is currently deprecated.

The code below:

local UserInputService = game:GetService("UserInputService")

local gui = script.Parent


debounce = false

game.Workspace.TouchPart1.Touched:connect(function(hit)
	if debounce == true then return end
    	debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			gui.Value.Value = true
		end
	wait(2)
	debounce = false
end)



debounce2 = false

game.Workspace.TouchPart2.Touched:connect(function(hit)
	if debounce2 == true then return end
    	debounce2 = true
		if hit.Parent:FindFirstChild("Humanoid") then
			gui.Value.Value = false
		end
	wait(2)
	debounce2 = false
end)



gui.Value.Changed:connect(function(NewValue)  -- I theorised that if i used this it would make the gui stop dragging but obviously it didnt work :/
	if NewValue == true then

          --- got this code for dragging on a forum post in this website --

local dragging    
local dragInput
local dragStart
local startPos

local function update(input)
	local delta = input.Position - dragStart
	gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
end

gui.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		dragging = true
		dragStart = input.Position
		startPos = gui.Position
		
		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				dragging = false
			end
		end)
	end
end)

gui.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		dragInput = input
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if input == dragInput and dragging then
		update(input)
	end
end)

else  --

	end)

The answer may be very obvious but i dont know what to do. I have also tried to make the gui variable equal to nil when the bool value is turned to false to stop it from being dragged while it did work it caused errors in the output and doesnt work when i try make the gui draggable again.

This is my first post on this website so excuse me if posted this in the wrong section or the layout of this post isnt very good.
Thanks in advance.

3 Likes

Move everything outside the Changed event, you don’t need it. Just check if gui.Value.Value in the update function before setting the position.

1 Like

The easiest way to solve this is to save a connection to your UserInputService.InputChanged event whenever the bool value changes to true. When the bool value changes to false, all you have to do is disconnect the connection and that should be it.

local connection

gui.Value.Changed(newValue)
    if newValue == false and connection then
        connection:Disconnect()
    elseif newValue == true then
        connection = UserInputService.InputChanged --...
    end
end)

Wow I made so many mistakes

As posatta said, you can put all the gui event connections, like gui.InputBegan:Connect, outside of the gui.Value.Changed event all together

1 Like

It wont let me put a tick on both of yalls answers. thank you so much!1!!

1 Like