Hi, I was making a team randomization script for my game, and none of the ones I tried on the internet worked. So I worked up from the basics of randomizing numbers to where I am now. Here's my code
local random = math.random(1,4) local player = game.Players:GetPlayers() if random <= 3 then player.Team = game.Teams.TeacherBlu player.TeamColor = game.Teams.TeacherBlu.TeamColor elseif random == 4 then print("Didthiswork") end
So this script is being called on and functioning because when I run the game the output has a 1/4 chance of printing "didthiswork". So the randomization is working. But if I get a value greater than or equal to three then nothing happens. My character doesn't switch teams, it doesn't spit out an error, nothing. So I've deduced that this stems from a problem with the game trying to put the player into a team. The problem is, I don't know what I'm doing wrong. can anyone tell me what's wrong with this part of the script? Thanks in advance.
player.Team = game.Teams.TeacherBlu player.TeamColor = game.Teams.TeacherBlu.TeamColor
GetPlayers returns a table with every player currently in the game, not any individual player. You can go through the table and set the team of every player in the table with a for loop
for i, player in pairs(game.Players:GetPlayers()) do local random = math.random(1, 4) -- get a new random number for every player, otherwise everyone will be on the same team! if random <= 3 then player.Team = game.Teams.TeacherBlu -- setting the TeamColor is not necessary else print("random number is 4") end end
or, you can use PlayerAdded to set a player's team when they join
game.Players.PlayerAdded:Connect(function(player) local random = math.random(1, 4) if random <= 3 then player.Team = game.Teams.TeacherBlu else print("random number is 4") end end)