How do I only get the ID from assets?

Hello, i am trying to only get the ID from an asset. What I mean by this is that instead of getting *http://www.roblox.com/asset/?id=***[ID]**, I only want the [ID]. Now I have tried to use string.gsub() to replace it but, that didn’t seem to work because i only got it to work with singular symbols or just one word, I have no idea why.

Also, the initial idea was to get the players hats and then parent them to a new humanoid. Now that didn’t seem to work either because for some reason no matter what i did it wouldn’t set itself to the humanoid although i would use :AddAccessory. It would be great if anyone has a method that works for this :slight_smile:

Thanks for reading, that is all.

You could do:

print(string.sub(YOURMESHTARGET.MeshId,14)) -- It will print everything starting from the 14th character which is the 1st number of the id

My original idea was to get the player’s Hat MeshId, But in this case the numbers are totally randomized depending on when the asset was uploaded.

You can use string.match and patterns to do this.

local url = 'rbxassetid://123456789'
local id = string.match(url, '%d+')

print(id) --> 123456789
2 Likes

Yep! I actually looked at the this not long ago, but I didn’t read into what it did. So I figured match meant it could only find anything that matches the input. :+1:

If you want the player’s hat MeshId then you could do:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
plr.CharacterAppearanceLoaded:Connect(function(char)

	local humanoid = char:WaitForChild("Humanoid")
	
	-- save hats for later
	local accessories = {}
	for _, accessory in pairs(humanoid:GetAccessories()) do 
		table.insert(accessories, accessory:Clone())
		
		-- get hat ID
		local handle = accessory:FindFirstChild("Handle")
		print(string.sub(handle.MeshId, 33))
	end
end)
end)

image flkfv’s idea seems to be a lot better than list guessing the sub number. Since it focuses on removing everything but the numbers. Or else it just prints whatever has 33? Is my guess.

No it types out the player’s hat ID, no guessing “33” means the 1st number