I want random choose three maps for player voting but I don't know how scripting that and I get errors.. So somebody can help me?
Workspace.GameScript:227: bad argument #2 to 'random' (number expected, got table)
local Maps = {} local AllMaps = RStorageService.Maps:GetChildren() for i = 0, 3 do local map = (AllMaps[math.random(1, AllMaps)]).Name for index, v in pairs(Maps) do if v == map then i = i - 1 return end end table.insert(Maps, map) end print(Maps[1], Maps[2], Maps[3])
PROBLEM #1
On line 5 of your script, you're telling the function math.random()
to choose a number between 1 and a table of all your maps. You're telling the function to choose a number between a number and table. This will result in an error, because the function expects both a and b to be number values.
HOW TO FIX PROBLEM #1
If you have a table, you can get the count of the table by prefixing the table with a "#" For Example,
local myTable = {"a",2,"hi"} print(#myTable) --Prints "3" in the output bar
To fix your script, simply prefix AllMaps
(inside of math.random) with a "#"
local Maps = {} local AllMaps = RStorageService.Maps:GetChildren() for i = 0, 3 do local map = (AllMaps[math.random(1, #AllMaps)]).Name --Notice the "#" for index, v in pairs(Maps) do if v == map then i = i - 1 return end end table.insert(Maps, map) end print(Maps[1], Maps[2], Maps[3])
If this doesn't fix your problem, I dont have any other solutions for you. Glad I could help!
-ReadyHappiness
PROBLEM #2
"Yes my error is fixed, but now my print in line 15 return (nil, nil, nil).. Do you know why?" -NiniBlackJackQc
Line 15 of your code goes something like this:
print(Maps[1], Maps[2], Maps[3])
-
On line 12, your code table.insert(Maps, map)
is correct, because the function's format, table.insert(table,item)
fits.
But you only did this once for the one map out of the 3 given.
So of course .., Maps[2], Maps[3]..
would print nil. So why is ..Maps[1],..
printing nil?
HOW TO FIX PROBLEM #2 - RECCOMMENDATION
Right now, line 15 prints this: nil nil nil
This is odd, because object 1 of Maps SHOULD be the map.
I do not know why Maps[1]
prints nil
. But here's a reccommendation.
Use the map
variable instead of Maps[1]
. Unless there's a reason that the map is inserted into the Maps
table, this alteration should solve your problem.
If you need further help, comment back on this answer.
Glad to help ya, again,
-ReadyHappiness
its not RStorageService its
game:GetService("RStorageService")