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

Why doesn't this math.random work in a script but does work in the command line?

Asked by
21_T 0
6 years ago

Hello, I am encountering an error when I try to run this simple piece of code

randomPlayer = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())]
game.ReplicatedStorage.Variables.ChosenPlayer.Value = randomPlayer
game.ReplicatedStorage.Variables.ChosenPlayerName.Value = randomPlayer.Name

When I enter this

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

in the command line, it works fine.

Any ideas on how to fix this? Filtering is enabled.

~ 21_T

0
Could be because the script runs before any player has connected. RubenKan 3615 — 6y
0
WaitForChiks User#19524 175 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
wait(3) --if Player isnt connected and script is working then .. script cant find randomplayer.
randomPlayer = game.Players:GetChildren()
[math.random(1,#game.Players:GetChildren())]
game.ReplicatedStorage.Variables.ChosenPlayer.Value = randomPlayer
game.ReplicatedStorage.Variables.ChosenPlayerName.Value = randomPlayer.Name
Ad

Answer this question