Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Why my Choosing Random Player Script not Working?

Asked by 6 years ago

Hello, I am trying to do make a good game but why is my Choosing Random Player script is wrong?

Script:

local randomPlayer = game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())]

Output:

20:09:07.220 - ServerScriptService.Core.GameScript:12: bad argument #2 to 'random' (interval is empty)
0
I know what that error means hold on Griffi0n 315 — 6y
0
ok FrezeTagger 75 — 6y

2 answers

Log in to vote
0
Answered by
Griffi0n 315 Moderation Voter
6 years ago

Your problem is that the amount of players in the server is 0

repeat wait() until #game.Players:GetChildren() >= 1
local randomPlayer = game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())]
0
Its worked! Thanks! Do you have Discord? Can i get your tag? FrezeTagger 75 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Unsure, but I've seen such scripts be like:

local randomPlayer = game.Players:GetChildren()[math.random(2, #game.Players:GetChildren())]

Notice how I replaced 1 with 2 !

0
Its not worked. FrezeTagger 75 — 6y
0
Hmm... LordTechet 53 — 6y

Answer this question