Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Argument 1 is missing or nil when trying to print an asset's name?

local audios = script.Parent
wait(5)

game.Workspace["Driving in car interior"]:Play()

IDs = {audios.AlongTheWatchTower, audios.BasketCase, audios.BlueSky, audios.CivilWar, audios.ClearlyNow, audios.ParadiseCity, audios.SeenTheRain, audios.Someday, audios.Starman, audios.TakeOnMe, audios.WhenIComeAround, audios.HighwayToHell, audios.EveryBodyWantsToRuleTheWorld, audios.ForWhatItsWorth, audios.Revolution, audios.OutOfTouch}

while true do
	local rnd = math.random(1,#IDs)
	for i = 1, #IDs do
		if i == #IDs then
			IDs[rnd]:Play()
			print(IDs[rnd])
			local id = tonumber(IDs[rnd].SoundId)
			local asset = game:GetService("MarketplaceService"):GetProductInfo(id)
			print(asset.Name)
		end
	end
	wait(IDs[rnd].TimeLength/IDs[rnd].PlaybackSpeed)
	print(IDs[rnd].TimeLength)
end

Hi, I have written a script to play a random audio from the table above but I cant seem to get it printing the name of the asset into the console, anyone know why?

1 answer

The SoundId property of a Sound instance defaults to "rbxassetid://ID". You cannot effectively cast a String to a number unless the was String is strictly a number beforehand; it will return nil.

This means your GetProductInfo call will not return any data about your asset due to the said nil-return.

To resolve this problem, we can simply extract the numerical figures via a Regular Expression:

local ID = tonumber(IDs[rnd].SoundId:match("[%d]+"))

Personally, I find your randomizer to be inefficient. If you wish to use my rendition of a Jukebox-like program, feel free to check it out here.

2 comments

0

I get an error when I try and implement that code, "match is not a valid member of Sound"

0

Apologies, I made a rookie mistake, I've edited the code!

This discussion is preserved as it was. New answers, comments, and votes are closed.