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

Problem with math.random Player Choosing. Anyone to help?

Asked by 5 years ago

Hello!

This is my script as of now:

local randomPlayer = game.Players:GetPlayers()
[math.random(1,#game.Players:GetPlayers())]
randomPlayer.TeamColor = BrickColor.new("Really blue")

However, when I run the game, an following error appears:

17:57:25.323 - Workspace.ao oni chooser:2: bad argument #2 to 'random' (interval is empty)

Can anyone please help? Thanks!

0
Why do you have [] (Square Brackets) in line 2? Zafirua 1348 — 5y
0
It's the index of the table returned by GetPlayers(). Rheines 661 — 5y
0
oh you mean its in the same line? O ok. Zafirua 1348 — 5y

1 answer

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago

Your error happens because the second argument in

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

can equate to zero when the script runs first before any player gets in the game, making Players:GetPlayers() return a table of length zero.

You will get the same error with

math.random(1,0)

To circumvent this error, I recommend that that portion of the script to be run only when after a player is added into the player service.

0
I did that, but now the script acts like it doesn't exist. Nothing in output, nothing happens. radzik9988 -7 — 5y
Ad

Answer this question