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.