Help with math.random

I am working on an among us type game, but I’m having some errors with math.random.
Script:

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

Error:
ReplicatedStorage.MainModule:9: invalid argument #2 to ‘random’ (interval is empty)

local Players = game.Players:GetPlayers()

local Imposter = Players[math.random(1, #Players)]

Try this

It still gives me the same error

local PlayerTable = game:GetService("Players"):GetPlayers()

local TerroristOne = PlayerTable[math.random(1,#PlayerTable)]

I am making a Trouble in terrorist town and I scraped it from this. I dont know why it says that error, Can you send your whole module script?

local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')

local module = {}

function module.ChooseImposters()
	local plrs = players:GetPlayers()
	local imposter = plrs[math.random(1, #plrs)]

	return imposter
end

what is the exact error again?

I dont think this will cause a issue but why are you returning imposter?

so i can get the imposter using another function

ReplicatedStorage.MainModule:9: invalid argument #2 to ‘random’ (interval is empty)

Can you please print

print(#plrs)

before imposter

This error pretty much says that the second argument(interval) is less than the first argument or nil
Are you sure your function isn’t being run before there is a player in game?

Maybe try printing ‘#plrs’ if its zero you’ll need to wait for a player to get in the game (preferably more than 1 so they aren’t alone but just waiting for 1 will stop the error)

I actually used

repeat wait(.1) until game.Players.NumPlayers >= 1

so it fixed it, thanks for your help

https://developer.roblox.com/en-us/api-reference/property/Players/NumPlayers

NumPlayers is deprecated and its use isn’t advised as its support could be dropped at any moment.

repeat task.wait() until game.Players:GetPlayers() >= 1
1 Like
function choosePlrs(tab)
local plrs = tab:GetChildren()
local n = math.random(1,#plrs)
local imposter = plrs[n]
return imposter
end

idk if this works