Player spawning into wrong team

When the player spawns in, there are 4 possible teams. The player is assigned correctly, but spawns on the wrong spawn sometimes. If the player is killed, they respawn at the correct one. How can I make the player spawning in wait until the team has been assigned, so they spawn on the correct spawn plate?

local function assignpla(player)
	for r,t in pairs(game.Teams:GetChildren()) do
		if #t:GetPlayers() == 0 or #t:GetPlayers() == nil then
			local col = t --empty team
			local qwe 
			for x,y in pairs(script.Parent.Tycoons:GetChildren()) do
				if y.Name == col.Name then
					qwe = y.Name
				end
			end
			player.TeamColor = BrickColor.new(qwe)
		end
	end
end

I tried to put the player.TeamColor = BrickColor.new(qwe) after qwe = y.Name and then a break but it doesn’t break out of all the loops

game.Players.PlayerAdded:connect(function(player)
	assignpla(player)

I would kill them when they spawn, since there’s a loading screen, but I don’t want that kill sound effect

Im not directly helping your issue, but you should write cleaner code for your own good in the later stages of development. This is a decent interpretation of what your code is supposed to do (I cannot really read it tbh, as you are abbreviating way too much):

local function assignTeamColor(player)
	for _, team in ipairs(game.Teams:GetChildren()) do
		if #team:GetPlayers() == 0 then
			local matchingTycoon = script.Parent.Tycoons:FindFirstChild(team.Name)
			
			if matchingTycoon then
				player.TeamColor = BrickColor.new(team.Name)
			end
            player:LoadCharacter()
            break
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	assignTeamColor(player)
end)

I also added a break which might fix it.

1 Like

You can use player:LoadCharacter() to reset character silent

Code:

local function assignpla(player)
	for r,t in pairs(game.Teams:GetChildren()) do
		if #t:GetPlayers() == 0 or #t:GetPlayers() == nil then
			local col = t --empty team
			local qwe 
			for x,y in pairs(script.Parent.Tycoons:GetChildren()) do
				if y.Name == col.Name then
					qwe = y.Name
				end
			end
			player.TeamColor = BrickColor.new(qwe)
		end
	end

    player:LoadCharacter()
end
1 Like

Perhaps you could turn off CharacterAutoLoads then when the TeamColor is assigned then you can use player:LoadCharacter() since there is no guarantee that the team will be set before the character is loaded.

character will not respawn anymore if CharacterAutoLoads is off. Yes, you can turn it on then, but it affects on every player on server

that’s why I said use player:LoadCharacter()

1 Like

Maybe I not understood it when i read. I don’t speak English primary