Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

5 comments

0

Yes my error is fixed, but now my print in line 15 return (nil, nil, nil).. Do you know why?

1

I will try to figure this out. Happens to me alot where i fix one thing and another's cap pops off.

0

P.S. Is the script your running your code in a "LocalScript" or a "Script?"

0

"Script", but I have fix my problem in remove "for index, v in pairs(Maps) do".. Thanks for answers:)

1

Okay, glad I could help you!

its not RStorageService its

game:GetService("RStorageService")

3 comments

0

In my script RStorageService is ( local RStorageService = game:GetService('ReplicatedStorage') )

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.

0

geez stop complaining im new to roblox scripting you dont have to be mean and complain on everything i post

This discussion is preserved as it was. New answers, comments, and votes are closed.