How to pick a random player from game.Players in a module script?

How do I get the Nextnteger to pick a random player from game.Players?
This is a module script btw

The error message I got from this module is: ServerScriptService.Script.GiveRole:5: attempt to get length of a Instance value


function module.ChooseCEO(player)
    local random = Random.new()
    local chooseCEO = player[random:NextInteger(1, #game.Players)]
    local SV = Instance.new("StringValue")
    SV.Parent = player
    SV.Name = "CEO"
    
    return chooseCEO
    
end

return module```

try local chooseCEO = #game.Players:GetChildren()[random:NextInteger(1, #game.Players:GetChildren())]

Tried it, heres the error I got:

ServerScriptService.Script.GiveRole:5: attempt to get length of a Instance value

Wait what did you mean to pass with player?

This is my approach if I were in your situation.

local selectedPlayer = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]

This gets a random player in the server. Replace game.Players:GetPlayers() with the table you wish to pick players from (If I understand your problem right)

I would use GetService() but I’m on mobile and I’m too lazy to type that

Neat! It worked! Thanks for the help man!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.