Help me with adding numbers?

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:
image
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.

Any help will be appriciated !

Make it

if (volume >= 1) then

Brackets are important fore comparing. And make it >= because > would only detecter if it’s higher. Not higher or equal

may be revers the ifs

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

Thanks for the reply.
Sadly putting it in brackets didn’t fix much.

And I forgot to update the code in my post, I did make it volume >= 0 after posting, so that’s been done and didn’t fix it.

EDIT: I’ve updated the code in my post

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.

2 Likes
elseif volume >=  0 and volume < 1 then

That all sounds very confusing to my little brain.
So you’re saying, my 0.1, 0.2, ect, aren’t actually 0.1 and 0.2 in human form?

Could I do math.floor(volume) or math.round(volume) and compare that?

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?

whyd you do the same if statement 2 times >= 0 would include >= 1

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

if volume <= 1 and > 0 then
      -- lower
end
if volume >= 1 and > 0 then
      -- increase
end

The script checks volume >= 1 first to see if its over 1, if its not it will check if its over 0 instead.

Can you send the full script then

Sure

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)	

Then I think the best solution is to save Volume as 0-10. Then when you set the audio, do Audio.Volume = Volume/10

1 Like

You never checked earlier? I don’t see it

I checked if its over one, then if it isnt, it’ll check if its over 0.

You didn’t check if its over 0 tho you did elseif

elseif does check if its over 0.

elseif (volume >= 0) then

yeah and your not checking if the >= 0 thing is actually < 0 your checking outside of the other if statement so its doing both