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

How to fix the "attempt to get length of a userdata value" problem on math.random ?

So, I'm making a game where there has to be a chosen map but when I try to use math.random on it, it gives me an error:

attempt to get length of a userdata value

Here's the script I used:

local Maps = game.ServerStorage.Maps

while true do
	local chosenMap = Maps[math.random(1, #Maps)]
	print(chosenMap.Name)
end

This is getting very annoying. Am I doing something wrong?

1 answer

✓ Accepted answer
local Maps = game.ServerStorage.Maps:GetChildren()

while true do
    wait()
    local chosenMap = Maps[math.random(1, #Maps)]
    print(chosenMap.Name)
end

2 comments

0

I'm very stupid I forgot to add :GetChildren(). Anyways, thanks for reminding! I hope I don't forget it again...

0

Haha, no worries.

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