catxkdb
(catxkdb)
February 25, 2022, 4:06am
1
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)
NDavis06
(オウオ)
February 25, 2022, 4:14am
2
local Players = game.Players:GetPlayers()
local Imposter = Players[math.random(1, #Players)]
Try this
catxkdb
(catxkdb)
February 25, 2022, 4:19am
3
It still gives me the same error
NDavis06
(オウオ)
February 25, 2022, 4:21am
4
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?
catxkdb
(catxkdb)
February 25, 2022, 4:23am
5
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
NDavis06
(オウオ)
February 25, 2022, 4:25am
6
what is the exact error again?
NDavis06
(オウオ)
February 25, 2022, 4:26am
7
I dont think this will cause a issue but why are you returning imposter?
catxkdb
(catxkdb)
February 25, 2022, 4:27am
8
so i can get the imposter using another function
catxkdb
(catxkdb)
February 25, 2022, 4:27am
9
ReplicatedStorage.MainModule:9: invalid argument #2 to ‘random’ (interval is empty)
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)
catxkdb
(catxkdb)
February 25, 2022, 4:33am
12
I actually used
repeat wait(.1) until game.Players.NumPlayers >= 1
so it fixed it, thanks for your help
Forummer
(Forummer)
February 25, 2022, 3:15pm
13
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
ianplus
(ianplus)
February 25, 2022, 3:21pm
14
function choosePlrs(tab)
local plrs = tab:GetChildren()
local n = math.random(1,#plrs)
local imposter = plrs[n]
return imposter
end
idk if this works