Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to implement a sound into my round timer?

Hey, I've been working on a round timer on one of my games. Originally, my friend made it, so I've been making some changes to it, however I'm running into some problems with this.

I want to add a sound during the intermission period at 5 seconds, and to end at once the round starts, however I'm running into problems doing this, so it'd be nice to have some help fixing this.

This is the code for the intermission

function intermission() -- Intermission
	game.ServerStorage.Counter.Value = 30	
	game.ServerStorage.GameState.Value = "Intermission"
	
	local initTimeValue = game.ServerStorage.Counter.Value
	local intermission = initTimeValue
	
	while(intermission >= 5)do
		Sound:Play()
		wait(5)
	
	while(intermission > 0)do
		wait(1)
		intermission = intermission - 1 
	    game.ServerStorage.Counter.Value = intermission
	end
  end
end

The variable I'm using for the sound

local Sound = game.Workspace.Sound

1 answer

✓ Accepted answer

You initialize the variable intermission to what ever value is stored in game.ServerStorage.Counter.Value when the function is first run. If this value is greater than 5, you will get stuck in a loop that continues forever as the variable intermission never changes.

From your description, I assume you want the sound to play the entire 5 seconds at the end of the intermission and stop when the timer reaches 0. In this case follow this code:

local Sound = game.Workspace.Sound

function intermission()
	local counter = game.ServerStorage.Counter
	counter.Value = 30

	local gameState = game.ServerStorage.GameState
	gameState.Value = "Intermission"
	
	while counter.Value > 0 do
		wait(1)
		counter.Value = counter.Value - 1

		if counter.Value == 5 then
			Sound:Play()
		end
	end
	Sound:Stop()
end

If you want the sound to play once each second until the timer reaches 0 (like a ticking sound, for example) you'll need a slight modification to line 14:

local Sound = game.Workspace.Sound

function intermission()
	local counter = game.ServerStorage.Counter
	counter.Value = 30

	local gameState = game.ServerStorage.GameState
	gameState.Value = "Intermission"
	
	while counter.Value > 0 do
		wait(1)
		counter.Value = counter.Value - 1

		if counter.Value <= 5 then
			Sound:Play()
		end
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.