Hi everyone! I’ve been trying to implement a slider bar to control the music volume of my game, but for some reason the button element of the UI doesn’t position on my mouse, I can’t figure out why - hopefully someone can help me understand what I am doing wrong!
Here is the relevant 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.Button
local db = 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
db = false
end
end)
script.Parent.Button.MouseButton1Down:Connect(function()
db = true
end)
Runservice.RenderStepped:Connect(function()
if db then
local MousePos = UIS:GetMouseLocation().Y
local BtnPos = Button.Position
local FrameSize = Frame.AbsoluteSize.Y
local FramePos = Frame.AbsolutePosition.Y
local pos = snap((MousePos - FramePos) / FrameSize, step)
percentage = math.clamp(pos,0,1)
Button.Position = UDim2.new(BtnPos.X.Scale, BtnPos.X.Offset, percentage, 0)
end
end)
Here is my UI structure:
