This script (regular script located in ServerScriptService) works randomly. Sometimes it runs, sometimes it does not run. I've been struggling to find out the problem. What would be the error in this code?

local gui = game.ReplicatedStorage.BillboardGui

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(char)
        repeat wait() until char
        for i,v in pairs(game.Players:GetPlayers()) do
            if v.TeamColor == plr.TeamColor then
                local bg = gui:Clone()
                bg.Parent = v:WaitForChild("PlayerGui")
                bg.Adornee = char:WaitForChild("Head")
            end
        end
    end)
end)