Numbervalue keeps subtracting numbers even after 0

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.

This time, it goes to weird number then 0 everytime it is cycled up and down from 0-0.3

Try replacing tostring(value) with tostring(defaultvolume.Value).

Hm. This time it cycles 0-1 normally but the weird number is still there regardless on what order you cycle in.

That seemed to fixed it. Thank you so much for all you guys help!

1 Like