I scripted with a knob dragging GUI, but the knob follows the cursor without clicking. Once I clicked, it releases a knob and it redo it again. So, look for an example video:
There is an example of a local script:
local frame = gui:WaitForChild("Frame")
local remoteReference = gui:WaitForChild("RemoteEventReference")
local eyeControl = frame:WaitForChild("EyeControl")
local eyeMain = eyeControl:WaitForChild("EyeMain")
local knob = eyeMain:WaitForChild("Knob")
local remote = nil
local faceModel = nil
local grabbed = false
local function calculateKnobPosition(inputPosition)
local XDiff = inputPosition.X - eyeMain.AbsolutePosition.X
local XRatio = math.min(math.max(XDiff / eyeMain.AbsoluteSize.X, 0), 1)
local YDiff = inputPosition.Y - eyeMain.AbsolutePosition.Y
local YRatio = math.min(math.max(XDiff / eyeMain.AbsoluteSize.Y, 0), 1)
return XRatio, YRatio
end
local function moveKnob(XRatio, YRatio)
local knobXScale = knob.Size.X.Scale / 2
local knobXOffset = knob.Size.X.Offset / 2
local knobYScale = knob.Size.Y.Scale / 2
local knobYOffset = knob.Size.Y.Offset / 2
knob.Position = UDim2.new(XRatio - knobXScale, -knobXOffset, YRatio - knobYScale, -knobYOffset)
end
local function mouseHeld(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or Enum.UserInputType.Touch then
grabbed = true
end
end
local function mouseReleased(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 or Enum.UserInputType.Touch then
grabbed = false
end
end
local function mouseMoved(input, gameProcessed)
if grabbed == true then
if input.UserInputType == Enum.UserInputType.MouseMovement or Enum.UserInputType.Touch then
local XRatio, YRatio = calculateKnobPosition(input.Position)
moveKnob(XRatio, YRatio)
remote:FireServer("RepositionEyes", faceModel, (XRatio - 0.5) * 2, (YRatio - 0.5) * 2)
end
end
end
local function onRemote(func, funcFaceModel, ...)
if funcFaceModel == faceModel then
if func == "GUIReposition" then
moveKnob(...)
initialPositionRecived = true
end
if func == "RequestCloseGUI" then
CloseGui()
end
end
end
knob.InputBegan:Connect(mouseHeld)
userInputService.InputEnded:Connect(mouseReleased)
userInputService.InputChanged:Connect(mouseMoved)
I also scripted with an UserInputService instance for making a dragging knob. So, how do I fix it?