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
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?
@theking107107Try 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.
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())