How do I make a "/playsong:id" command?

Hi there, I have a music game and I’m currently trying to figure out how to make a music command. My idea is that when you type “/play:123” it will set the song’s id to 123. I’ve tried using string.split and string.find to find the idea but I am absolutely lost and have no idea what to do.

local id

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local message = string.lower(msg)

		if plr.UserId == 1081939203 then
			if message == string.split(string.find(message,"/play:"),":") then -- undone, but my progress
				id = -- set to what the id was in the command
			end
		end
	end)
end)
1 Like

Hello @vxsqi Try this:

local id

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local message = string.lower(msg)
		local Args = string.split(msg, ":")
		
		if plr.UserId == 1081939203 then
			if Args[1] == "/play" then 
				id = Args[2]
				print(id)
			end
		end
	end)
end)

4 Likes

I’ll try it, also I tried something similar like this which actually prints the id out - but how do I concatenate "rbxassetid:// with string.match(message, "%d+")?

1 Like

Uhm… Once you get the id just connect it like this

id = Args[2]
local AudioURL = "rbxassetid://" .. id
1 Like

Yeah I tried that and it said string could not be converted into ‘number | string’

Use tostring(VarWords) method

1 Like
local numbers = string.match(message, "%d+")
id = "rbxassetid://"..tostring(numbers)

Fixed code:

local id

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local message = string.lower(msg)

		if plr.UserId == 1081939203 then
			local args = message:split(':')
            if (args[1]) then
               if args[1] == "/play" then
                   id = "rbxassetid://"..string.match(message, "%d+")
               send   
            end
		end
	end)
end)

What if someone were to say something before “/play”? How could I prevent the command from running if there is something before the /play?

for example: "blabla/play:`` – still receives the command

It will not work since I am not using string.find.
I am check if argument at index 1 is /play if its correct then continue.

1 Like
local Game = game
local Players = Game:GetService("Players")

local Admins = {1081939203}

local function OnPlayerAdded(Player)
	local function OnPlayerChatted(Message)
		if not table.find(Admins, Player.UserId) then return end
		local Id = string.match(string.lower(Message), "^/play:(%d+)$")
		if not Id then return end
		print(Id)
	end
	
	Player.Chatted:Connect(OnPlayerChatted)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

‘/play:{songId}’ is the command, for example ‘/play:1’ results in ‘1’ being printed to the console.

1 Like

image

AllowedUsers

image

MusicCreate

SoundManager

image

  • Exploiters can’t play sounds, only autorized users.
  • Client check and Server check the user id.
  • It even works for players who have just joined!

You can remove this: “print()” and “warn()”.

Thank you. - Kat望み#2090

1 Like

Thanks, I’ll mark this as the solution but I actually did something quite similar which works the same:

elseif splitmessage[1] == '/play' and splitmessage[2] then
				print(splitmessage[1],splitmessage[2])
				local id = tostring(string.match(splitmessage[2], "%d+"))
				print(id)
				local url = "rbxassetid://" .. id
				local SongInfo
				
				local success,err = pcall(function()
					SongInfo = MPS:GetProductInfo(id)
				end)
				
				if success then
					print("playing custom song")
					musicplayer.TimePosition = 0
					musicplayer.SoundId = url
					currentTrack.Value = "..."
					currentTrack.Value = SongInfo.Name
				else
					warn("id not valid")
				end
				
				print(id)
			end
1 Like