Numbers inserted into an array instead of strings

I’m trying to script an audio module for my games, both preloading and playing functions work, but I’m having some problems with the playlist playing function, my script is similar to the one that the Developer Hub has, but it has modifications, and I do understand the code.

I’m adding the AName (which is the audio name that was assigned in an LocalScript) which should be an string. The problem here is that the AName is considered an number, meaning that instead of my playlist being String1, String2, they are numbers, such as 1, 2.

What I have tried so far is to rewrite the table inserting and removing lines but no results. I also tried modifying the loops of my scripts, but still the same. I have also looked at the Tables and the Sounds and Music pages, but no results at all.

PlayPlaylist function (ModuleScript)
function AudioEngine:PlayPlaylist(AudioArray)
	local Playlist = {}
	
	for AName in pairs(AudioArray) do
		table.insert(Playlist, AName)
		print("DEBUG - Added to Playlist: ".. AName)
		
		AudioEngine:PlayAudio(Playlist[1])
		wait(Playlist[1].Ended)
		table.remove(Playlist, 1)
	end
end
AudioPlayer LocalScript (requires the module)
-- Engine is the ModuleScript, which is required at the start of the script, don't worry, didn't forget about it.

Engine:PreloadAudio({

["PanaMiguel"] = 5058774370,

["Kolors"] = 478553966

})

wait(3)

Engine:PlayPlaylist("Kolors", "PanaMiguel")
Explorer

Anotación 2020-07-06 234213

The audio preloading does work, but the problem is that the PlayPlaylist script inserts numbers in the Playlist table instead of the ANames.

Seriously kinda ashames me to ask an “why is this not working” question here, because I’m not that good at scripting, and can’t even call myself decent. :pensive:

So in roblox tables, you have key value pairs. In arrays the keys are incrementing numbers, in dictionaries they can be string values, or disordered numbers.

So lets look at this part:
for AName in pairs(AudioArray) do

pairs is a lua function which returns an iterator for the loop to go through. Right here, you’re just taking in the key, I think you might be looking for the value. So try

for key, AName in pairs(AudioArray) do

Edit: Also noticed, that you’re not passing an array into the function. You’re passing in multiple arguments.
Engine:PlayPlaylist("Kolors", "PanaMiguel") -- Passes in two string args
Engine:PlayPlaylist({"Kolors", "PanaMiguel"}) -- Passes in one table arg

2 Likes