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

This is my script for sorting teams and teleporting them that I made. It doesn't work. Any help?

The function doesn't work when It gets called, and I'm not getting any error messages, any help?

local players = game.Players:GetChildren()

function sortTeams()
	
		table.remove(game.Teams.SpecOnly:GetChildren())
		
		local RandomPlayer = math.random(game.Players:GetChildren())
		
		table.remove(RandomPlayer)
		
		local RandomPlayerTwo = math.random(game.Players:GetChildren())
		
		game.RandomPlayer.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(239.9, 5.962, 228.2))
		
		game.RandomPlayerTwo.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(118.9, 5.962, 196.2))
	end
	
while #players >= 2 do
	wait(5) -- Intermission Time
		sortTeams()
	end

1 answer

✓ 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

0

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

0

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

0

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.

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