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)
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)
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+")?
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)
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.
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