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

Random player chooser only works in Studio, how can I fix this?

I have a random player chooser script and basically, it will choose a random player and give him clothes and place him on a team. It worked fine in Studio but now it's gone to shreds, nothing is working. Here is the script:

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

Any ideas on how I an fix this?

2 comments

0

I don't see a problem. How did the script work in Studio?

0

It waited less than a second and put me on the team I specified since I didn't have anyone else. I tried it on the actual game and nothing happens, nothing appears in the console or anything. I tried remaking it and it worked fine in Studio, but yet again failed to work on the actual game.

2 answers

✓ Accepted answer

Nothing is happening in the actual game because line 2 will execute immediately when the game starts, however the player doesnt get addedd into the game that fast,so it will get nothing and the script will end, so what you can do is use a game.Players.PlayerAdded event listener and assign your player's teams in this listener

game.Players.PlayerAdded:Connect(function(plr)
	game.Workspace:WaitForChild(plr.Name)--this is to make sure he spawns first, this is optional
local randomPlayer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
randomPlayer.TeamColor = BrickColor.new("Deep orange")
end)


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