Hi there, I am working on a volume control button where if you click the button “-”, it subtracts the volumes from the sound playing and vice versa for “+”. The issue here is that the numbervalue that is used for showing the volume of the song in a text label keeps going below 0 even after when my if script section states not to.
The video below shows what is happening. Also notice for some odd reason “0” is read as a different number. Pay attention to the properties tab too.
local up = script.Parent.up
local down = script.Parent.down
local sound = workspace.Sound
local defaultvolume = game.ReplicatedStorage.volume
local volume = script.Parent.volume
volume.Text = " " .. defaultvolume.Value
up.MouseButton1Click:Connect(function()
if sound.Volume <= 1 then
sound.Volume = sound.Volume+.1
defaultvolume.Value = defaultvolume.Value+.1
volume.Text = " " .. defaultvolume.Value
else
sound.Volume = sound.Volume+0
defaultvolume.Value = defaultvolume.Value+0
volume.Text = " " .. defaultvolume.Value
end
end)
down.MouseButton1Click:Connect(function()
if sound.Volume >= 0 then
sound.Volume = sound.Volume-.1
defaultvolume.Value = defaultvolume.Value-.1
volume.Text = " " .. defaultvolume.Value
else
sound.Volume = sound.Volume-0
defaultvolume.Value = defaultvolume.Value-0
volume.Text = " " .. defaultvolume.Value
end
end)
Yeah, that should work. If not, you might need to be doing a little math to round the number out so it doesn’t get any super small decimals like 0.0000000014, which I’ll explain if it doesn’t work.
NumberValues aren’t unsigned so you should be expecting a negative value if you don’t write any code to constrain the value. Any of these are valid ways to do it:
Value = math.clamp(0, MAX_VALUE, CURRENT_VALUE)
Value = math.max(0, CURRENT_VALUE - DECREMENT)
Value = math.min(MAX_VALUE, CURRENT_VALUE + INCREMENT)
I prefer the latter two options when I’m predictably controlling my increments and decrements on objects and prefer the first one in certain circumstances such as using one event to update something that’s separate from my incrementers and decrementers. In your case, you should just use the latter two; the math.min for your up and math.max for your down.
Why would you need a NumberValue object in the first place? You can just let the buttons control the Sound.Volume directly and then set the TextLabel’s Text = " " ..Sound.Volume.
local parent = script.Parent
local up = parent.up
local down = parent.down
local sound = workspace.Sound
local rs = game:GetService("ReplicatedStorage")
local defaultvolume = rs.volume
local volume = parent.volume
volume.Text = " " .. defaultvolume.Value
up.MouseButton1Click:Connect(function()
if sound.Volume >= 0 and sound.Volume <= 1 then
sound.Volume += 0.1
volume.Text = " " .. defaultvolume.Value
end
if defaultvolume.Value >= 0 and defaultvolume.Value <= 1 then
defaultvolume.Value += 0.1
volume.Text = " " .. defaultvolume.Value
end
end)
down.MouseButton1Click:Connect(function()
if sound.Volume >= 0 and sound.Volume <= 1 then
sound.Volume -= 0.1
volume.Text = " " .. defaultvolume.Value
end
if defaultvolume.Value >= 0 and defaultvolume.Value then
defaultvolume.Value -= 0.1
volume.Text = " " .. defaultvolume.Value
end
end)
This will work for now, you may want two buttons, one for default volume & another for current volume.
@Forummer made a typo on the line if sound.Value >= 0 and sound.Volume <= 1 then it should be replaced with if sound.Volume >= 0 and sound.Volume <= 1 then
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local parent = script.Parent
local up = parent.up
local down = parent.down
local sound = workspace.Sound
local defaultvolume = ReplicatedStorage.volume
local volume = parent.volume
volume.Text = " "..tostring(defaultvolume.Value)
function apply_change(value)
--doesn't let it go above 1
value = math.min(1, value)
--doesn't let it go below 0
value = math.max(0, value)
--avoid floating point errors
value = math.round(value*10)/10
defaultvolume.Value = value
sound.Volume = value
volume.Text = " "..tostring(defaultvolume.Value)
end
up.MouseButton1Click:Connect(function()
apply_change(defaultvolume.Value+0.1)
end)
down.MouseButton1Click:Connect(function()
apply_change(defaultvolume.Value-0.1)
end)