How do I create a sliding GUI?

I’m trying to make a sliding GUI to alter the brightness of a PointLight.
I’ve looked at a few posts and found code to drag it, yet I’m not sure how to restrain it from leaving the area specified.
image

Code can be found on this post: Draggable property is hidden on gui objects - #4 by LMH_Hutch

The code I have so far thanks to @Tiffblocks :

local UserInputService = game:GetService("UserInputService")

local gui = script.Parent


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)
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)

3 Likes

I personally attempted to use this code on a slider, however this code works poorly in that specific use case. Especially when there’s no change, the slider resets since it is based on the change in position of the input object.

Check this out instead:

Also, the sliders are based on percentages (0-1) it should be multiplied by the actual value you want changed.

4 Likes

This is where we have to use the AbsoluteSize property:

local abs = (startPos.X.Scale * gui.Parent.AbsoluteSize.X) + startPos.X.Offset

if abs > gui.Parent.AbsoluteSize.X then
    gui.Position = UDim2.new(0, gui.Parent.AbsoluteSize.X, 0, 0)
elseif abs < 0 then
    gui.Position = UDim2.new()
end
3 Likes