How to connect a Slider Gui with a Sound?

Well I have the following script that works as a GUI slider and I want to merge it with the volume of a “Sound” so that when you move it the volume increases. And in the end I don’t know how to merge it as a sound, so I come to ask for help.

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 Sound = script.Parent:WaitForChild("Sound")
local db = false
local step = 0.001
local percentage = 0

function snap(number, factor)
if factor == 0 then
return
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().X
local BtnPos = Button.Position
local ButtonSize = Frame.AbsoluteSize.X
local ButtonPos = Frame.AbsolutePosition.X
local pos = snap((MousePos-ButtonPos)/ButtonSize, step)
percentage = math.clamp(pos, 0,1)
Button.Position = UDim2.new(percentage, 0, BtnPos.Y.Scale, BtnPos.Y.Offset)
end
end)

You can change the volume according to the number.

Sound.Volume = snap()

Something like this.

2 Likes