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