This is not how I would do this. I will show you how to do this using your approach though, but I will also show you a way to make this faster and more efficient.
If you try to find the index you just go over the whole table to find the value and then return that index. In other words if your table has 500 items it will take a max of 500 iterations. That is all thrown away CPU time which you could also spend on something else!
ads = {18941,132582,28539, 23586}
sounds = {189415,29356,289356, nil}
function FindIndex(table, value)
for index, val in pairs(table) do
if val == value then
return index
end
end
end
local index = FindIndex(ads, 132582)
print("Sound: "..sounds[index])
Note that tables are constructed with {} and not with [] :)
My approach would be to link the ads and the sounds together, so you don't need to find the right index.
It would look like this:
a = {{18941, 189415}, {132582, 29356}}
So a[2][1] is 132582; that's the ad. a[2][2] is the sound id: 29356.
I'll also provide a quick convertor.
ads = {18941,132582,28539, 23586}
sounds = {189415,29356,289356, nil}
local Catalog = {}
for i,v in pairs(ads) do
Catalog[i] = {v, sounds[i]}
end
function GetAd(index)
return Catalog[index] and Catalog[index][1]
end
function GetSound(index)
return Catalog[index] and Catalog[index][2]
end
print(GetAd(1), GetSound(1))
Note that the and operator is used to prevent errors when you try to index an ad which does not exist - if we don't do that your code will error, now the function just returns nil.
I hope this helped, if you have any questions about this code or approach feel free to comment.