What does this only work for one player?

I am trying to make player move and change their team Value between team 1 and team 2 but my code only changes one player, even if there are 2 players. Can someone please explain?

	local playerTable = game.Players:GetChildren()
	for k,player in ipairs(playerTable) do
		print(#playerTable)
		local PlayersTeam = player:WaitForChild("Team")
		local Character = player.Character
		if Team1 < Team2 then
			Team1 = Team1 + 1
			PlayersTeam.Value = 1
			Character:MoveTo(Team1Area.Position)
		elseif Team2 < Team1 then
			Team2 = Team2 + 1
			PlayersTeam.Value = 2
			Character:MoveTo(Team2Area.Position)
		else
			if math.random(1,2) == 1 then
				Team1 = Team1 + 1
				PlayersTeam.Value = 1
				Character:MoveTo(Team1Area.Position)
			else
				Team2 = Team2 + 1
				PlayersTeam.Value = 2
				Character:MoveTo(Team2Area.Position)
			end
		end
3 Likes

does your print command print twice or once ?

1 Like

only once and that is why i’m asking

1 Like

what does it print out ? does it print out 2 players or only 1 ? print(playerTable) make the print like this and check if there are 2 players

1 Like

it prints the amount of players but only once

1 Like

change it to the new print, so you see players inside the table

1 Like

it printed this
â–Ľ {
[1] = Player1,
[2] = Player2
}

1 Like

hmm, weird, try debugging if you know how to do that

1 Like

i do not, might i ask how to debug it?

1 Like

Hopefully this will help you understand how to do it. It basically allows you to go step by step trough code and check what it does

1 Like

Is your code running while two players are in the game?

If you ran that code at the start of a script, for example, only 0-1 players would be in the game initially.

You might need to use PlayerAdded to run code for each player that joins the game.

its runs when all players are loaded in

can it be because it is done on a script and not a local one?

This shouldn’t be a problem.

Is it possible you’re getting an infinite yield on:

So the loop never continues?

how odd if i delete most of the code but leave the print it prints two times

it doesn’t say anything on infinite yield error

Your code is probably yielding infinitely. For example, if you start a while wait loop, that will block the code from continuing, or if you have a WaitForChild but the child never exists.

If one part of the loop waits/yields forever, then the next iteration will never happen.

Yielding doesn’t always give an error, since it can be intended behavior. Is that the full code of your loop?

@theking107107 Try adding print statements throughout the iteration code to see if it stops anywhere, I would bet it does.


Either takes a similar amount of time but one is easier for newer developers.

but it does exists
image

I suggest adding a print statement for each variable, which are your Team instance and Character instance, to check if they exist. And then add a print statement after each :MoveTo to confirm if the code block successfully runs if the variables are no problem. Do note that instances still take time to load in the game even if it already exists in the beginning, as they load in only after the player instance loads in, (this is especially so for the player.Character, which can yield the script, and why I usually do: local Character = player.Character or player.CharacterAdded:Wait())

Find the problem, it was a local function called HowMuchleft() but i don’t know why it was to active after the loop