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

MapVoter not work, bad argument #2 to 'random' (number expected, got table)?

Asked by 6 years ago

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])

2 answers

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago

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

0
Yes my error is fixed, but now my print in line 15 return (nil, nil, nil).. Do you know why? NiniBlackJackQc 1562 — 6y
1
I will try to figure this out. Happens to me alot where i fix one thing and another's cap pops off. ReadyHappiness 109 — 6y
0
P.S. Is the script your running your code in a "LocalScript" or a "Script?" ReadyHappiness 109 — 6y
0
"Script", but I have fix my problem in remove "for index, v in pairs(Maps) do".. Thanks for answers:) NiniBlackJackQc 1562 — 6y
1
Okay, glad I could help you! ReadyHappiness 109 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

its not RStorageService its

game:GetService("RStorageService")
0
In my script RStorageService is ( local RStorageService = game:GetService('ReplicatedStorage') ) NiniBlackJackQc 1562 — 6y
1
If you inspect the error NiniBlackJackQc gives, it doesn't say that that particular line throws an error. While the error doesn't show which line, it's pretty easy to find out where the error is, and (if you have good scripting knowledge) the fix TO the error. ReadyHappiness 109 — 6y
0
geez stop complaining im new to roblox scripting you dont have to be mean and complain on everything i post User#20192 0 — 6y

Answer this question