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

How to select a(n) item from a table by math.Random or something else ?

Asked by 4 years ago

So i have a table containing musicid:

local table = {"26616646","516165131","151656548"} -- they are not real (if they are real...idk)
local Random = math.Random(1,#table)
print(Random)

at first I thought that it will print the music id but it is printing 1, 2,3.... So how do i get the music id to be print out randomly ? Please help me as i am a very bad scripter... Thanks for reading :D

1 answer

Log in to vote
0
Answered by 4 years ago

Change it to this:

local table = {"26616646","516165131","151656548"}

local Random = math.Random(1,#table)

local ChosenSong = table[Random]

print(Random)

table gives the number of objects in the table, in your case, 3. Math.random returns a number and not an object or a word.

0
Oops sorry change the "Random" at line 7 to "Chosen song TheRealPotatoChips 793 — 4y
0
The reason it was printing 1, 2, 3 was because the hashtag mean't numbers of items in your table. JesseSong 3916 — 4y
0
oh ok i will test it..thx Nguyenlegiahung 1091 — 4y
0
@JesseSong ...As i said.. i am very bad at scripting :) Nguyenlegiahung 1091 — 4y
0
@JesseSong are you rewriting my answer? Cuz we don't copyright here TheRealPotatoChips 793 — 4y
Ad

Answer this question