How can I make a slider UI end at a certain point?

Basically, What I mean is I have a UI slider in my game that controls the fov, But I have encountered an error. Basically I want to make it so when you put it to the left, It caps at 40, Like seen in this image.
image
Also a bonus would be if its possible to cap it at 120? Instead of 100. rn its just 0-100 but I was looking to make it 40-120!

help would be appreciated :slightly_smiling_face:

(Code)

local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local Runservice = game:GetService("RunService")
local Frame = script.Parent
local Button = script.Parent.TextButton
local db = false
local step = 0.001
local percentage = 70

function snap(number,factor)
	if factor == 0 then
		return number
	else
		return math.floor(number/factor+0.5)*factor
	end
end

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		db = false
	end
end)

script.Parent.TextButton.MouseButton1Down:Connect(function()
	db = true
end)

Runservice.RenderStepped:Connect(function()
	if db then
		local MousePos = UIS:GetMouseLocation().X
		local BtnPos = Button.Position
		local FrameSize = Frame.AbsoluteSize.X
		local FramePos = Frame.AbsolutePosition.X
		local pos = snap((MousePos-FramePos)/FrameSize,step)
		percentage = math.clamp(pos,0,1)
		Button.Position = UDim2.new(percentage,0,BtnPos.Y.Scale,BtnPos.Y.Offset)
	end
end)

I added minPercentage and maxPercentage variables to define the minimum and maximum allowed percentages for the slider. I set them to 40% and 120%. The math.clamp function makes sure that the slider stays within the 40% and 120%.

local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local Runservice = game:GetService("RunService")
local Frame = script.Parent
local Button = script.Parent.TextButton
local db = false
local step = 0.001
local minPercentage = 0.4  -- 40% as minimum value
local maxPercentage = 1.2  -- 120% as maximum value

function snap(number, factor)
    if factor == 0 then
        return number
    else
        return math.floor(number / factor + 0.5) * factor
    end
end

UIS.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        db = false
    end
end)

script.Parent.TextButton.MouseButton1Down:Connect(function()
    db = true
end)

Runservice.RenderStepped:Connect(function()
    if db then
        local MousePos = UIS:GetMouseLocation().X
        local BtnPos = Button.Position
        local FrameSize = Frame.AbsoluteSize.X
        local FramePos = Frame.AbsolutePosition.X
        local pos = snap((MousePos - FramePos) / FrameSize, step)
        
        -- Applying changes to cap the slider between 40% and 120%
        percentage = math.clamp(pos, minPercentage, maxPercentage)
        
        Button.Position = UDim2.new(percentage, 0, BtnPos.Y.Scale, BtnPos.Y.Offset)
    end
end)

local minFOV, maxFOV = 40, 120
Runservice.RenderStepped:Connect(function()
	if db then
		local MousePos = UIS:GetMouseLocation().X
		local BtnPos = Button.Position
		local FrameSize = Frame.AbsoluteSize.X
		local FramePos = Frame.AbsolutePosition.X
		local pos = snap((MousePos-FramePos)/FrameSize,step)	
		local percentage = math.clamp(math.clamp(pos,0,1),0,1)
		local cappedFOV = minFOV+percentage *(maxFOV-minFOV)
		Button.Position = UDim2.new(percentage,0,BtnPos.Y.Scale,BtnPos.Y.Offset)
		
		print(cappedFOV)
	end
end)
1 Like

Actually, this is probably more accurate.

local backgroundWidth = 400 --slider background width
local minFOV, maxFOV = 40, 120
Runservice.RenderStepped:Connect(function()
	if db then
		local MousePos = UIS:GetMouseLocation().X
		local BtnPos = Button.Position
		local FrameSize = Frame.AbsoluteSize.X
		local FramePos = Frame.AbsolutePosition.X
		local pos = snap((MousePos - FramePos) / FrameSize, step)
		local percentage = math.clamp(pos, 0, 1)
		local cappedFOV = minFOV + percentage * (maxFOV - minFOV)
		local sliderWidth = Button.AbsoluteSize.X
		local cappedPosition = (cappedFOV - minFOV) / (maxFOV - minFOV) * (backgroundWidth - sliderWidth)
		Button.Position = UDim2.new(0, cappedPosition, BtnPos.Y.Scale, BtnPos.Y.Offset)

		print(cappedFOV)
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.