What do you want to achieve?
I want to create a slider that you can adjust the volume of sound effects with.
What is the issue?
The slider goes beyond the bar on the right side.
What solutions have you tried so far?
I did look for solutions on the forum, but those people didn’t seem to have the same problem as me.
This is the code for the slider:
local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local bar = script.Parent.Bar
local box = script.Parent.Number
local slider = bar.Slider
local active = false
local step = 0.01
local percentage = 0
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
active = false
end
end)
slider.MouseButton1Down:Connect(function()
active = true
end)
RS.RenderStepped:Connect(function()
if active then
local MousePos = UIS:GetMouseLocation().X
local spos = slider.Position
local bsize = bar.AbsoluteSize.X
local bpos = bar.AbsolutePosition.X
local pos = snap((MousePos-bpos)/bsize,step)
percentage = math.clamp(pos,0,1)
slider.Position = UDim2.new(percentage,0,spos.Y.Scale,spos.Y.Offset)
box.Text = string.format("%.0f%%", percentage * 100)
end
end)
local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local bar = script.Parent.Bar
local box = script.Parent.Number
local slider = bar.Slider
local active = false
local step = 0.01
local percentage = 0
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
active = false
end
end)
slider.MouseButton1Down:Connect(function()
active = true
end)
RS.RenderStepped:Connect(function()
if active then
local MousePos = UIS:GetMouseLocation().X
local spos = slider.Position
local bsize = bar.AbsoluteSize.X
local bpos = bar.AbsolutePosition.X
local pos = snap((MousePos-bpos)/bsize,step)
percentage = math.clamp(pos,0,1)
slider.Size = UDim2.new(percentage,0,spos.Y.Scale,spos.Y.Offset)
box.Text = string.format("%.0f%%", percentage * 100)
end
end)
This will not fix my problem of the slider going beyond the bar, as changing .Size to .Position will just make the script be the same as it originally was.
As for an alternative to Renderstepped, I’m not very experienced in making UIs so I cannot say anything about that, unfortunately.
I found a solution! Changing slider.Position = UDim2.new(percentage,0,spos.Y.Scale,spos.Y.Offset) to slider.Position = UDim2.new(percentage,-12.5,spos.Y.Scale, spos.Y.Offset) will fix the issue of the slider going beyond the bar.