✓ Accepted answer
I'm not quite sure what the intention of this script is. But at the moment, every 5 seconds the script will run the sortTeams() function. Although looking at it, game.Players:GetChildren() is defined at the very start of the script and will remain at that same table (which would be empty or possibly have only one player) for the entire game.
You will want to do:
while (#game.Players:GetPlayers() >= 2) do
wait(5)
sortTeams()
end
Although this will still run it every 5 seconds.
As for your sortTeams() function. Line 5 is irrelevant because you don't use the value or the table. For "RandomPlayer", you need to use the '#' to get the length of the table returned by :GetChildren(), then index the same table with that number, so:
local PlayersTable = game.Players:GetChildren()
local RandomPlayer = PlayersTable[math.random(#PlayersTable)]
And when you assign the Player's position, what you actually want is the character's, so:
RandomPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(239.9, 5.962, 228.2))
You should also check to make sure .Character and .HumanoidRootPart exist before running it or the script will error.
local PlayerChar = RandomPlayer.Character
if (PlayerChar and PlayerChar:FindFirstChild('HumanoidRootPart')) then
PlayerChar.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(239.9, 5.962, 228.2))
end
To change a Player's Team, you will need to change their Team property to the Team object in game.Teams:
RandomPlayer.Team = game:GetService('Teams').Team1
3 comments
The Intention of the script is to select two random players and then teleport them into an arena. I want to filter out the specOnly team to use as a "Spectate Only" function, so I believe Line 5 has purpose. I added in the scripts you suggested (except for the teams one, as the team spawns have Team change on touch) and the script still doesn't call the function
EDIT: forgot to add the PlayersTable variable and to replace the RandomPlayer variable, so line 5 is useless. The sortTeams function still doesn't work though
table.remove removes a specific index from a table. First argument is the table you want to remove from, the second is the index that you want to remove.