How to Improve my Music Player

Hey Developers!
So I am making a music player for my airline, it chooses a random song, and then plays it, they are all in my music folder. It works fine, but I might need to either skip a song, or stop it, and then be able to resume the song, so I have made several commands, /Stop which stops the song, and doesn’t resume it until someone types /Resume also, I made it so that the player must be a certain role in my group, and I made a /Skip command. However, even thought I made a function for these, How can I make them actually work?

Here is my script:

--Music Player

local Stopped = false
local MusicFolder = game.Workspace.Music
local Status = game.ReplicatedStorage:WaitForChild('Status')

function PlayRandomSong()
	local ChosenSong = MusicFolder:GetChildren()[math.random(1, #MusicFolder:GetChildren())]
	ChosenSong.Playing = true
	Status.Value = ChosenSong.Name
	wait(ChosenSong.TimeLength)
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		if msg == '/Stop' or '/stop' and Player:GetRankInGroup(5875291) >= 9 then
			--This is where I will need to stop the music
		elseif msg == '/skip' or '/Skip' and Player:GetRankInGroup(5875291) >= 9 then
			--This is where I will need to skip the current music
		elseif msg == '/Resume' or '/resume' and Player:GetRankInGroup(5875291) >= 9 then
			if Stopped == true then
				--Resume the music if it has been stopped
			end
		end
	end)
end)

while true do
	wait(1)
	PlayRandomSong()
end



Thanks for help!

1 Like

For /stop you can store the time the song is at and then when you use resume you play it from that time again.

/skip you can simply change the id. aka just run PlayRandomSong again

Can you give an example of how to do this?

/stop and /resume

sound:Pause() -- Pauses without resetting the time

sound:Resume() -- Plays from where it left off.

--/skip
sound:Stop()
PlayRandomSong()
1 Like

Additionally, this:

if msg == '/Stop' or '/stop'

Could become:

msg = string.lower(msg)
if msg == "/stop"
elseif msg == "/skip"
elseif msg = "/resume"

Just to make it easier for you to handle.

2 Likes

So basically, I modified m script so it looks like this:

--Music Player

local Stopped = false
local MusicFolder = game.Workspace.Music
local Status = game.ReplicatedStorage:WaitForChild('Status')

function PlayRandomSong()
	local ChosenSong = MusicFolder:GetChildren()[math.random(1, #MusicFolder:GetChildren())]
	ChosenSong.Playing = true
	Status.Value = ChosenSong.Name
	wait(ChosenSong.TimeLength)
	game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		if msg == '/Stop' or '/stop' and Player:GetRankInGroup(5875291) >= 9 then
			--This is where I will need to stop the music
			ChosenSong:Pause()
		elseif msg == '/skip' or '/Skip' and Player:GetRankInGroup(5875291) >= 9 then
			--This is where I will need to skip the current music
			ChosenSong:Stop()
			PlayRandomSong()
		elseif msg == '/Resume' or '/resume' and Player:GetRankInGroup(5875291) >= 9 then
			if Stopped == true then
				--Resume the music if it has been stopped
				ChosenSong:Resume() 
			end
		end
	end)
end)
end


while true do
	wait(1)
	PlayRandomSong()
end



But none of the commands work.

If this code already works you should put it in the #help-and-feedback:code-review category.

Do not mix the chatted with the PlayRandomSong, make them separate.
You need to go to the sound object in Workspace or wherever you placed it, not the table value.

Hi! Do you know how to implement this into my game? I like the script, just not able to get it working. Can you help? Thanks!