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

Random map picker not working?

Asked by 4 years ago
local function randomMap()
    local randMap = game.ServerStorage.Maps[math.random(1, #game.ServerStorage.Maps:GetChildren())]

    return randMap
end

The line local randMap = game.ServerStorage.Maps[math.random(1, #game.ServerStorage.Maps:GetChildren())] raises an error saying 1 is not a valid member of Folder. I don't understand why this is happening.

Any help will be greatly appreciated :)

3 answers

Log in to vote
3
Answered by 4 years ago

The problem is that the table actually holds the contents and not "how much of the table".

I would also add variables so it makes a bit easier to read.

The reason why you got "1 is not a valid member of folder" is because your trying to access a children named "1" in the folder.

local MapsFolder = game.ServerStorage.Maps

local function randomMap()
    local randMap = MapsFolder:GetChildren()[math.random(1, #Maps:GetChildren())]

    return randMap
end

Also check if theres 2 or more maps in the MapsFolder, or else math.random() won't work because you can't randomize objects with a single quantity.

tl;dr: you forgot to get a table of the folder

1
math.random() supply's a pseudo-random number which is used to index** the map within the array; each table sorts the content at a ascending numerical placeholder, this is why we can use that function of the Math library in this scenario. To actively attain these maps within the array, we have to use the :GetChildren() method, this of which you forgot. Without it, you’re essentially calling Instan Ziffixture 6913 — 4y
0
Ok thanks! Kataclysmix 23 — 4y
0
also, #Maps:GetChildren() should be MapsFolder:GetChildren() correct? Kataclysmix 23 — 4y
0
Nope. When you use the hashtag operator, your basically getting "how much is in a table" when you put it next to a table value. That's why we pick maps based on how much maps there is. 123nabilben123 499 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

Your problem is in line 2, You did game.ServerStorage.Maps which is supposed to be game.ServerStorage.Maps:GetChildren(). Please do research before asking next time, Here is the fixed code:

local function randomMap()
       local Maps = game.ServerStorage.Maps:GetChildren()
       local randMap = items[math.random(1, #items)]

       return randMap
end
Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

You have to make an in pairs loop. Also, it says 1 is not a valid member of folder is because the script is trying to find a child of the folder called 1.

local function randomMap()
    local random = math.random(1, #game.ServerStorage.Maps:GetChildren())

    for i, v in pairs(game.ServerStorage.Maps:GetChildren()) do
        if i == random then
            randMap = v
        end
    end 

    return randMap
end
1
This is overcomplicated, just get the children of the folder as a table. simple. NickIsANuke 217 — 4y
0
Okay, I didn't know that. youtubemasterWOW 2741 — 4y

Answer this question