Music command thingy

I’m making this command based music changer. I’m having problems with one of the commands. I want it to get the asset id “rbxassetid://00000000” from the end of "/changeMusic " and put it in a variable. Short explanation:

  • A player says “/changeMusic rbxassetid://00000000” in the chat.
  • We want to get the “rbxassetid://00000000” off of that message, and store it in a variable.
    Code (This is in a local script.):
local function musicFunction()
    local music = Instance.new("Sound")
    music.Parent = game.Workspace

    music.Name = "Cynacol's Music"
    music.SoundId = "rbxassetid://342274063"
    music.Looped = true
    music.Volume = 2
end

local player = game.Players.Cynacol
player.Chatted:Connect(function(msg)
    if string.sub(string.lower(msg), 1, 10) == "/stopMusic" then
        music:Stop()
    end
    if string.sub(string.lower(msg), 1, 10) == "/playMusic" then
        music:Play()
    end
    if string.sub(string.lower(msg, 1, 13) == "/changeMusic " then
        -- In here is where we want to obtain the "rbxassetid://00000000" off of the player, and store it in the music's SoundId.
    end
end)
1 Like

local audioId = string.split(msg, "/changeMusic ")[2]

what is this meant to do? Please explain.

The function string.split() splits a string into parts based on the specified delimiter (2nd string value argument passed to the function), in this case I’ve split the bit you don’t want from the bit you do want. The returned value is an array of strings, we want the 2nd element of that array so “[2]” is used to index the 2nd item of that array. Which in turn is the part of the string you want.

so it will cut the rbxassetid part away from it? And how would I store the rbxassetid bit in a variable?

I already did that in the above “local audioId =”.

There is bit more info on string.split() here.

oh oops i understand now. Sorry :slight_smile:

Would [2] seperate the “/” away from the string?

You’ll get “rbxassetid://00000000” returned which is what you wanted right?

alright. ill test it out now if it works.

Alternatively, you can use string.match()

local audioId = string.match(msg, "rbxassetid://[0-9]+")

This is better than string.split, because if your making a /msg command andput spaces in the arguments, it will only show one word, so string.match is better.