How do I get the sound to rise slowly until a certain level is achieved?

What it does

This script is meant to put a sound into a folder in workspace and slowly raise the volume until it reaches at certain number.

The issue

So, when I play this script… it does everything correctly except it doesn’t raise the volume of the Volume but instead plays the highest volume instantly.

Tactics I’ve Used

I’ve tried changing through the while () do and repeat multiple times, changing the volume of said sound and it doesn’t work.

NOTE

  1. Yes, everyone is supposed to be able to trigger this… hence line 15.
  2. Everything but the volume rising works, I have no idea what i’m doing wrong.

Script itself

--Made By MillerrIAm
--------Variables-------
SoundName = "Cheer"
SSound = game:GetService("ServerStorage").CrowdSounds
WSound = game:GetService("Workspace").CrowdSounds
----------- Messages -----------
msg1 = "c" 
msg2 = "C"
msg3 = "cheer" 
msg4 = "CHEER"
--------Main Code------
game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg == msg1 then
			if WSound:FindFirstChild(SoundName) then
			else
				SSound[SoundName].Volume = 0
				SSound[SoundName]:Clone().Parent = WSound
				WSound[SoundName]:Play()
				repeat
					WSound[SoundName].Volume = WSound[SoundName].Volume + 1
					wait(0.5)
				until WSound[SoundName].Volume == 1
				wait(10)
				WSound[SoundName]:Stop()
				WSound[SoundName]:Destroy()
			end
		end
	end)
end)

Error Line

-- This is the section of the script that I'm having issues with. Line 20 to 23.
				repeat
					WSound[SoundName].Volume = WSound[SoundName].Volume + 1
					wait(0.5)
				until WSound[SoundName].Volume == 1

Last Words

Thank you to anyone who takes the time to look over this and help me achieve my goal for this.

1 Like
repeat
	WSound[SoundName].Volume = WSound[SoundName].Volume + 1
	wait(0.5)
until WSound[SoundName].Volume == 1

You are adding 1, so it instantly becomes 1 assuming it is 0 at start.

Use a numeric for loop, using a lower increment:

for i = 0, 10 do
    WSound[SoundName].Volume = i/10
    wait(0.5)
end

Notice I did not use the step argument here. Some users have complained about floating point errors, so I deliberately avoided it.

Nitpicks

  1. No need to write your name at the top of the script. Nobody else is gonna see it.
  2. Please use local variables. Usage of global variables is bad practice, it makes your code messy.
  3. Use for loops if you need to do something a set amount of times (as I have)
  4. Use variables (more of them) to not repeat yourself. You’re doing WSound[SoundName] everywhere.
  5. Empty if blocks are bad practice. Invert the condition instead.
3 Likes
  1. This script is going to be released to the public, if an error occurred… people can come to me for help. It will be uploaded to a group and since i’m making it… its better that people know who actually made the script so they can be helped.
  2. Please describe what you mean by local variables and what part of the script you mean…
  3. Got it.
  4. Duly noted, it becomes hard to remember to make more variables as I end up forgetting that I should add it and I end up just writing extra code.
  5. I used an empty if block due to the if not part having issues.
  1. Ok

  2. Local variables are declared by prefixing the identifier (variable name) with the local keyword, i.e.

    local variable = 5
    

    Globals are bad practice because you might encounter name collisions.

    Need an example?

    local function test()
        x = math.random(1, 5)
    end
    
    test()
    print(x) -- might print 3
    test()
    print(x) -- might print 5
    

    Every time the function was called, it wrote to the same variable. Code using it after the function might not work as intended because of this. Variables should have the shortest lifespan possible.

    Note that

    local x = 5
    x = 6
    

    Does NOT make x a global variable. It just reassigns it to a new value. Global variables are declared (initialized, i.e. set for the first time) without the local keyword. Also, local variables are faster to access than globals. But that is not why you use locals. You use them because they are good practice.

  3. Ok

  4. Ok

  5. What do you mean? Instead of this:

    if x then
    
    else
        print("x")
    end
    

    Do this:

    if not x then
        print("x")
    end    
    

    Eliminates the need for an else block in the first place.

  1. Okay, thank you for explaining.
  2. I tried that originally and it didn’t work, I fixed that part though.
  3. I have another question, how would I reverse the index to lower the volume?

for i = 10, 0, -1 do

Here, using the step argument (the last number) is fine since it’s an integer. Remember to divide by 10 since Volume is not a percentage!

I’m assuming you mean something like this…

				for i = 10,0,-1 do
					WSound[SoundName].Volume = i/-1
					wait(0.5)
				end

I also tried

				for i = 10,0,-1 do
					WSound[SoundName].Volume = 10/-1
					wait(0.5)
				end
  1. The first one instantly makes it go to zero and I need it to slowly go down
  2. It goes up into a nil value…

Apologies, I have no idea where to learn this index feature.

No, divide by 10. on both. 10/-1 is always -10.

1 Like

Okay, that makes more sense… Where do I learn more about indexes? I tend to stray away from them as I have no idea where to look and the stuff on the DevForum is usually on others scripts…

normal for loops are formatted as

for i = startNumber, endNumber, increment