Title really says it all, I want to make all players on one team but only one, randomly picked.
I am good at the randomly picking part and it works I think, but the team doesn’t get assigned, and there is no error output. here is the script:
local plyrs = game:GetService("Players"):GetPlayers()
local char = plyrs[math.random(1,#plyrs)]
local Players = game:GetService("Players")
local player = Players:FindFirstChild(char.Name)
local children = game.Players:GetChildren()
for i = 1, #children do
children.Team = game.Teams["Survivor"]
if children.Name == char.Name then
player.Team = game.Teams["Killer"]
end
end
It’s not working because “children” is a table of all players, and in the loop you’re just adding a key “Team” to the players table. You need to be setting the teams of each actual player in the table. You can get the player from the for loop by indexing the “children” table with the current value of i, e.g.
local Player = children[i]
And then setting the team of that player.
Also there’s no need to get the players table twice if you’re not changing them, a lot of your code is redundant.
This
local plyrs = game:GetService("Players"):GetPlayers()
local char = plyrs[math.random(1,#plyrs)]
local Players = game:GetService("Players")
local player = Players:FindFirstChild(char.Name)
local children = game.Players:GetChildren()
could just be this
local Players = game:GetService("Players")
local PlayersTable = Players:GetPlayers()
local char = PlayersTable[math.random(#PlayersTable)]
Thanks for the new variables + tips on how to shorten code, but where is this supposed to go? Is this supposed to be a new variable? A replacement for a redundant variable?
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local PlayersTable = Players:GetPlayers()
local RandomPlayer = PlayersTable[math.random(#PlayersTable)]
-- for loop that goes from 1 to number of players
for i = 1, #PlayersTable do
local Player = PlayersTable[i]
-- if player is the random player we picked
-- then set their team to Killer
if Player == RandomPlayer then
Player.Team = Teams["Killer"]
-- otherwise set their team to Survivor
else
Player.Team = Teams["Survivor"]
end
end
You could also just use pairs to make your life easier:
for i, Player in pairs(PlayersTable) do
if Player == RandomPlayer then
Player.Team = Teams["Killer"]
else
Player.Team = Teams["Survivor"]
end
end