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