Numbervalue keeps subtracting numbers even after 0

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)

What happen was a floating point rounding error, really annoying but I can fix it by using integers than just dividing by 10 to turn into decimal.

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 curVolInt = math.round(defaultvolume * 10)

up.MouseButton1Click:Connect(function()

curVolInt += 1

curVolInt = math.max(curVolInt,0,10)

volume.Text = " " .. string.format("%.1f",curVolInt/10)

defaultvolume.Value = curVolInt

end)

down.MouseButton1Click:Connect(function()

curVolInt -= 1

curVolInt = math.max(curVolInt,0,10)

volume.Text = " " .. string.format("%.1f",curVolInt/10)

defaultvolume.Value = curVolInt

end)

what about truncating the number?
with this function, you don’t even need to round the number

local function truncate(n, to)
   return n-n % 10^(-to)
end

print(truncate(0.1234567, 1)) --> 0.1
print(truncate(0.1234567, 2)) --> 0.12

Incorrect, the issue is related to the if statement being wrong(the if volume >= 0 and volume <= 1 ignoring the other possibilities of it being -0.1 for example(with the wrong version of the code above it can get this value if you lower the volume when its set to 0), this also applies to the 1 statement(the value can be 1.1).

No. If you watched the video, you see before the zero that it goes to a crazy small number like 0.00000005323 like that, because of a floating point rounding error. Keeping it a int as long as possible keeps bad things from happening.

This is a thing, but Roblox most of the time rounds the numbers and the error is NOT related to that.

what is it then?

aDQWFDSs

I told you above. basically I’m going to over-simplify the steps the old reply did:

  1. If the volume is >= 0 and <= 1 increase with +0.1
  2. when it reaches one it’s equal to 1 so it increases it(it becomes 1.1)
  3. after that the statement cannot run again cause the volume is above 1(therefore the volume locks in place)

the main mistake here is that 1 of those checks should be made for each condition(subtracting and adding) and do not allow the volume variable to get larger than 1 or lower than 0.

Well my code is clamped so if the values trying to be set get bigger or lower than 0 to 1 then it would clamp it to 0 or 1.

It’s fixed now, when I was copying and pasting your code I left a typo in.

I get this error here, what is actually happening?

Players.vxsqi.PlayerGui.Settings.GUI.panels.General.Music Volume.LocalScript:13: attempt to perform arithmetic (mul) on Instance and number

Try using my solution instead:

After cycling down, the weird number appears, but going up and down again goes to 0, after cycling back to top and down it goes to 0 properly without going to the weird number.

Try again with the updated code snippet, I think the issue was related to my code not using tostring for the UI text.