Any solutions to making the next song play on a key press

You can write your topic however you want, but you need to answer these questions:

  1. So I’m making a radio system and when the player presses the right bracket, the next song should play.

  2. Unfortunately, it only plays one song and hitting the key plays the song again. I’m not getting any errors.

  1. I’ve tried looking at multiple tutorials on loops, changing the format of the for loop. Nothing worked.

Basically, I’m trying to have the song change by pressing the right bracket. Any suggestions would be helpful.

Thanks.

1 Like

try this:

local currentIndex = 1

local function onInputBegan(input, gameProcessed)
   if input.KeyCode == Enum.KeyCode.RightBracket then
   currentIndex += 1
   if currentIndex > 10 then
     currentIndex = 1
   end
   track.SoundId = snd[currentIndex].id
   track:Play()
  end
end

to be honest i am not really sure you need to use a for loop for that

1 Like
local soundToPlay = 0
local function OnInputBegan(input, gameProcessed)
	if gameProcessed then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.RightBracket then
		soundToPlay += 1
		if soundToPlay > #snds then
			soundToPlay = 1
		end
		
		local info = string.match(track.SoundId, "%d+")
		track.SoundId = snds[soundToPlay].id
		track:Play()
	end
end

Use an upvalue/non-local variable to log which song to play, increment its value by 1 each time the button is pressed then reset it back when the last song in the table is reached , remember to use the “gameProcessed” parameter to ignore user inputs which were in relation to the core features of the game (chat for example).

1 Like

Thanks, this works perfectly. I went about this the wrong way. I’ve also managed to make the previous songs play using the left bracket.

2 Likes