Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why my Choosing Random Player Script not Working?

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)

2 comments

0

I know what that error means hold on

0

ok

2 answers

✓ Accepted answer

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())]

1 comment

0

Its worked! Thanks! Do you have Discord? Can i get your tag?

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 !

2 comments

0

Its not worked.

0

Hmm...

This discussion is preserved as it was. New answers, comments, and votes are closed.