Numbervalue keeps subtracting numbers even after 0

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)
1 Like

You check if its less than or equal to 0, which means when it equals 0 you can continue to lower it. Simply change it to > and it should fix it.

Also, you can just do

sound.Volume -= .1

instead of doing the full thing. You can also do +=, *=, and /= for addition, subtraction, and multiplication.

like this?

if sound.Volume > 0 then

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.

Yeah I was about to say, it doesn’t stop at 0 and stops at -1, but when you go above 0 and then go back down it stops at -2. Weird behaviour.

My current script:

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)

Try just setting this to

	else
		sound.Volume = 0
		defaultvolume.Value = 0

This is weird. Please watch the video below.

Can you send the script again? That looks like its still using >= since it can go below once.

You could just make it so if it goes under zero it goes to zero

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 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 = 0
		defaultvolume.Value = 0
		volume.Text = " " .. defaultvolume.Value
	end
end)

Try this:


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

local currVol = defaultvolume.Value

up.MouseButton1Click:Connect(function()

currVol += 0.1

currVol = math.clamp(currVol,0,1)

sound.Volume = currVol

defaultvolume.Value = currVol

volume.Text = " " .. currVol

end)

down.MouseButton1Click:Connect(function()

currVol -= 0.1

currVol = math.clamp(currVol,0,1)

sound.Volume = currVol

defaultvolume.Value = currVol

volume.Text = " " .. currVol

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

Value is not a valid member of Sound “Workspace.Sound”

There’s this weird number that shows up before 0.1. On the numbervalue it shows as 0.

@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

just did it, but now it stops at -0.1

also, you can’t change the volume after that (in terms of the textlabel, the volume still works)

I did some bug fixing to the code above:

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)