How can I get only the actual ID from a music SoundId?

So, I need to get only the ID from the sound ID in a music system for my game. However, in order to do this, I need to remove the “rbxassetid://” part. Any ideas as to how I can do this?

1 Like

use string.split() there are probably other ways, but this is the one I know.

local str = "rbxassetid://101010101"

print(string.split(str, "rbxassetid://")[2])
1 Like

This might not be the best way but it is easy to implement

id = tonumber(yoursound.SoundId:sub(14))
1 Like

Make a table of all the music ID’s you need first. Then loop through them and get the results. This works well if you have a lot of ID’s you want to convert.
Image showing result:
image

Code:

--List of music ID's
local musicList = {"rbxassetid://5410086218", "rbxassetid://1837324424", "rbxassetid://7029061992"}

--An example of how to add more id's to the ID list of songs.
local song4 = "rbxassetid://1837461008"
table.insert(musicList, song4)

--Then you need to make another table that will hold the result
local musicListIDs = {}

--This is a basic for-loop to split the string from the first half so we are only left with the music ID
for i, music in ipairs(musicList)do
	
	--Splitting the ids
	local musicID = music:split("rbxassetid://")
	
	--Inserting the result of the split in the musicListIDs table
	table.insert(musicListIDs, musicID[2])
	
	--Printing the individual IDs as an example
	print(musicID[2])
end

--Printing the table as an example
print(musicListIDs)

It can’t be a table, as I have a user-submitted sound ID system as well.

If you look at the code, there is a part that lets that be implemented.

local musicList = {"rbxassetid://5410086218", "rbxassetid://1837324424", "rbxassetid://7029061992"}
--An example of how to add more id's to the ID list of songs.
local song4 = "rbxassetid://1837461008"
table.insert(musicList, song4)

Just do table.insert(musicList, songThatThePlayerSubmitted) and it will append into the array and work fine.

string.match("rbxassetid://123456789", "%d+") --> 123456789
string.match("123456789asd", "%d+") --> 123456789
string.match("ioasjdfaksjd123456789aksdgjkahhj", "%d+") --> 123456789
3 Likes

This worked, thank you so much. Also, I’m sorry for just reading the “make a table” part of your reply lol.

1 Like