Attempt to get length of global 'playerJoiningGameFolder' (a userdata value)

*** What are you attempting to achieve? (Keep it simple and clear)**
Choose a random character and get the name of the character.

*** What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)**
I get this error: [attempt to get length of global ‘playerJoiningGameFolder’ (a userdata value)]

solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)
Tried a lot of different ways of getting a random character.

Hi, basically I have a folder with players that want to join a game. And I want to get a random character from that folder so I can teleport the player. Somehow it doesnt work, I think it might be something with that I want to make a character a variable…

Here is the script:
(more, but not important stuff)

playerJoiningGameFolder = workspace.PlayersJoiningGame

function chooseRandomPlayerFromList()
	 local randomPlayer = math.random(1,#playerJoiningGameFolder) --error here
	 return randomPlayer
end

full error: Workspace.VotingSystemServer.RoundChooserMainScriptServer:317: attempt to get length of global ‘playerJoiningGameFolder’ (a userdata value)]

thank for any help!

1 Like
playerJoiningGameFolder = workspace.PlayersJoiningGame

function chooseRandomPlayerFromList()
     local players = playerJoiningGameFolder:GetChildren()
     local randomPlayer = players[math.random(1, #players)]
     return randomPlayer
end
3 Likes

To explain @bmcq_12’s solution:

You are trying to use # to get the length of an object, which isn’t possible.
The # operator can only be used to get the length of tables.

In the solution above you’re checking the length of the table of all the children of the playerJoiningGameFolder.

7 Likes