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

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

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

2 comments

0

Could be because the script runs before any player has connected.

0

WaitForChiks

1 answer

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

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