When testing on a local server, this script runs for the first player, but not the second player?

-- assign plr to team
print("running team assign")
local char = script.Parent
local plr = game.Players:GetPlayerFromCharacter(char)





if plr.Team.Name ~= "TeamQueue" then
	print(plr.Name.."already in a team")
	return

elseif plr.Team.Name == "TeamQueue" then


	local teams = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
    local players = team:GetPlayers()
    
if #players <= 0 then
	plr.Team = team
	print(plr.Name.." assigned to "..team.Name)
	break
elseif #players >= 1 then
	return
end
end
end
--

this script is in the characterstarterscripts folder.
this script is supposed to get all the teams in the game, then check each one to see if there is 1 player in it or not. if there is no player then the player that joins the game is assigned to it.

when I run the game on a local server, the first player that joins is assigned to an empty team, but when I add another player to the server, the second player is stuck in “TeamQueue” and never gets assigned to an empty team.

the script for the second player only prints out “running team assign”

Hello, there was 1 little thing in your script:

  • If you remove the elseif #players <= 0, your script functions well. You don’t need those 2 lines because the if statement already checks if the team doesn’t have a player. If it doesn’t, it continues on and if there’s nothing after, it just loops to the next one.

Here’s the code to use:

print(script.Name .. ": Running team assign")
local character = script.Parent
local plr = game.Players:GetPlayerFromCharacter(character)

if plr.Team ~= game.Teams.TeamQueue then
	print(plr.Name ..": Already in a team")
	return
elseif plr.Team == game.Teams.TeamQueue then
	local teams = game:GetService("Teams"):GetTeams()
	for _, team in pairs(teams) do
	    local players = team:GetPlayers()
		if #players <= 0 then
			plr.Team = team
			print(plr.Name.." assigned to "..team.Name)
			break
		end
	end
end

Hopefully this helps!

1 Like

I would consider adding a slight delay before starting the script because replication is not always on time which can lead to some problems. Try adding a wait() or maybe even a wait(0.05).

1 Like

I apologize if I forgot to mention that this is a normal script, not a localscript, so using localplayer throws an error. even when changing it back to GetPlayerFromCharacter(), the second player still gets stuck at “TeamQueue”, only the first player goes to a new team.

Why use a server script in StarterPlayer? Plus I just tested the updated script in my game, it works with a LocalScript… Just put the code in a LocalScript and put the LocalScript in StarterPlayerScripts.

but what about Filtering enabled, wont changing your team locally be ignored by the server?

I edited my first reply. Try that code in StarterCharacterScripts.

I kept the elseif in the code before, I just edited again. Sorry about the constant edits. Try now.

it still didnt work even after the changes, I think the problem is the for loop,

when the game starts, all teams have 0 players, when the first player joins, the script iterates to the first team, there are no players in it so the first player gets put in it. now the second player joins, the loop iterates and comes to the first team again, this time it has 1 player in it, and instead of iterating to the next team, it freezes and does nothing.

so for some reason it stops working when it comes across a team with a player in it.

EDIT: tried your final changes and it seems to be working now(we posted comments at the same time)

1 Like