Search code examples
luaroblox

Is there an easy way of choosing a random person to be assigned to a team in Lua/Roblox Studio?


I have not found any solutions to the issue I'm having. I am currently attempting:

local SniperTeam = game.Teams.Sniper

if #game.Players:GetPlayers() < 2 then
repeat
wait()
until #game.Players:GetPlayers() > 1
end

local players = game.Players:GetPlayers()
local playerChosen = math.Random(1, #players)
playerChosen.Team = game.Teams.Sniper

And I get no errors, but when I attempt it in game, it doesn't work.


Solution

  • It's math.random (lower case m)

    https://www.lua.org/manual/5.3/manual.html#6.7

    Unrelated, but you can simplify your repeat loop using

    while #game.Players:GetPlayers() < 2 do
        wait()
    end