So I am currently trying to make a script that sets a the local players music volume.
Here’s how I am doing that:

if (volume >= 1) then
print(volume.." is higher or equal to 1")
volume = 0
elseif (volume >= 0) then
print(volume.." is lower than 1")
volume += 0.1
end

By default, volume is set to 1.

If the number is the already max (1), it will reset the volume back to 0.
However my issue is that, that isn’t happenining.

Here is the output of me changing the volume:

As you can see, it will say that 1 isn’t equal to 1, and it will add 0.1 onto the volume making it 1.1 which will make some complications later on.

if (volume >= 0) then
print(volume.." is lower than 1")
volume += 0.1
elseif (volume >= 1) then
print(volume.." is higher or equal to 1")
volume = 0
end

0.1 in binary is 0.00011001100 (repeating infinitely)
Obviously computers can’t store a number that repeats infinitely, so it gets rounded. This rounded number is (in 32 bit) 0.100000001490116119384765625
This obviously isn’t .1
If you do a lot of math by adding and subtracting decimal numbers, you can make this error worse.
The solution is to store volume as an integer volume += 1 and then when you set the volume divide by 10. This way you’re not working with error-prone decimals.

I suppose you might be able to, but you still need to be multiplying or dividing by ten in the end. What’s the smallest increment you adjust the volume by? Is it just .1? Or do you ever change the volume by .05 or something?

The volume is only ever increased by 0.1, due to the fact I can’t be bothered to make a volume slider lol
So the player just clicks a button and a function is ran that changes the volume

local volBtn = ico.new()
volBtn:setImage("rbxassetid://7849985039")
volBtn:set("iconBackgroundColor", Color3.fromRGB(31,31,31))
volBtn:set("iconBackgroundTransparency", 0)
volBtn:setRight()
volBtn:setCaption(((math.floor(game.Workspace.CurrentSong.Volume))*100).."%")
local volInst = Instance.new("NumberValue", game.Players.LocalPlayer)
volInst.Name = "Volume"
volInst.Value = 1
volBtn.selected:Connect(function()
volBtn:deselect()
if (volume >= 1) then
print(volume.." is higher or equal to 1")
volume = 0
elseif (volume >= 0) then
print(volume.." is lower than 1")
volume += 0.1
end
game:GetService("TweenService"):Create(game:GetService("Workspace").CurrentSong, TweenInfo.new(0.3), {
Volume = volume
}):Play()
volInst.Value = volume
warn("Volume has been set to "..volume)
-- set preloaded imgs
local muted = "rbxassetid://7849985287"
local sml = "rbxassetid://7849984870"
local big = "rbxassetid://7849985039"
if volume >= 0.7 then
volBtn:setImage(big)
local roundedVolume = roundNumber(volInst.Value, 1)
elseif volume <= 0.6 and volume > 0 then
volBtn:setImage(sml)
elseif volume == 0 then
volBtn:setImage(muted)
end
end)