Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would i make a loop choose a song name along with the id in a table?

Asked by
I_Nev 200 Moderation Voter
4 years ago

I know this may be a stupid question but i want to know how to make a table with the songs name and the song id along with it like this(This is just music i test with it)

local Songs = {
    ["Old Town Road"] = 2862170886,
    ["Verzache - Needs"] = 3777427220,
    ["You Better Run"] = 2352608398,
    ["Truncate - WRKTRX 3"] = 2352639731,
    ["Wheats"] = 2639964810,
    ["SPOOKY SCARY SKELETONS"] = 160442087,
    ["Revenge"] = 627722878
}

I want to be able to have the script get the song name, and fire that to the server to show all clients what is playing in a text lable(I already have all that set up) but i can't get the table to give the name and the id so i can set the id in the script and make it play while telling everyone whats playing. for one it just returns nil or it will give me just the song id, i have tried everything i know to do and it fails.

this is the function i am using to get the song id, and this keeps returning an error saying " bad argument #2 to 'random' (interval is empty)"

function PickSong()
    local Song = Songs[math.random(1,#Songs)]
    local picked = Songs[Song]
    print(picked)
end
0
I made a song gui that was REAALLY similar to this if you want to use it? royaltoe 5144 — 4y

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

The way you have it, doesn't allow you to get indices of a table.

Visually, you're doing this:

Songs = {
    Old Town Road = 2862170886,
    Verzache  Needs = 3777427220,
    --etc.....

}

Songs[1] does not exist because it is a dictionary and dictionary don't have incidences.

What you could do is this:

Songs = {
    {SongName = "Old Town Road", AssetId = 2862170886},
    {SongName  = "Verzache  Needs", AssetId = 3777427220},
    --etc.....

}

print(Songs[1].SongName, " ", Songs[1].AssetId)
print(Songs[2].SongName, " ", Songs[2].AssetId)

--getting a random song
local randomSong = Songs[math.random(1, #Songs)]
print(Songs[randomSong].SongName, " ", Songs[randomSong ].AssetId)


Songs[1] does exist in the form of a table. Songs[1].SongName is Old Town Road.

0
Thank you guys, both of them worked flawlessly. Sorry i just couodnt figure out what i was doing wrong, tha k for the help :) I_Nev 200 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

The reason for nil is because you are trying to find an element in the table that's indexed (keyed) by a number (from 1 to #songs). which none exists ofcourse. they are indexed by their "names" (what's in between []).

If I were you I would use a table like this:

local Songs = {
    {["Old Town Road"], 2862170886},
    {["Verzache - Needs"], 3777427220},
    {["You Better Run"], 2352608398},
    {["Truncate - WRKTRX 3"], 2352639731},
    {["Wheats"], 2639964810},
    {["SPOOKY SCARY SKELETONS"], 160442087},
    {["Revenge"], 627722878}
}

so now you can just:

function PickSong()
    local Song = Songs[math.random(1,#Songs)]
    local picked = Songs[Song]
    print("the name for the song is: "..tostring(picked[1])) -- since we didn't specify lua will just make the keys for use, which starts from 1. which is now the name for the song (the beginning of the table picked from Songs)
    print("the ID for the song is: "..tostring(picked[2])) -- same for this one but instead it's the second element
end

If you are having any more trouble, please let me know :D

Answer this question