I am new, i have a problem when excute 2 comman, need help

i have a problem when Add a user to a channel, the comand add-TeamUser works , but Add-teamchannelUser is not., with error code.
Add-TeamChannelUser : Error occurred while executing Add-TeamChannelUser
Code: NotFound
Message: User is not found in the team.
Pls help to clarify my script
thanks

Add-TeamUser -GroupId $teamgroupid -User $UserName
Add-TeamChannelUser -GroupId $teamgroupid -DisplayName “$ChannelName” -User $UserName

function Add-UserToChannel
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $UserName,
        [Parameter(Mandatory=$true)]
        [string] $ChannelName
        
    )
    Process{
        $i = 0
        foreach($teamgroupid in $($allteamgroups.Groupid))
        {
            $i++
            $teamname = Get-Team -GroupId $teamgroupid
            Write-Progress -Activity "Finding All channels from $($teamname.DisplayName)" -Status "$i out of $($allteamgroups.Count) completed"
            $allteamchannel = Get-TeamChannel -GroupId $teamgroupid
            foreach($channelN in $($allteamchannel.DisplayName))
            {
                if($ChannelName -like $channelN)
                {
                    $TeamgroupName = Get-Team -GroupId $teamgroupid
                    $checkchannelUserName = Get-TeamChannelUser -GroupId $teamgroupid -DisplayName $ChannelName
                    if($($checkchannelUserName.User) -contains $UserName){
                      Write-Host("User already in $ChannelName") -ForegroundColor Green 
                    }else {
                            $checkteamUser = Get-TeamUser -GroupId $teamgroupid
                            If($checkteamUser.User -contains $UserName){
                                Write-Host("Adding $UserName to $ChannelName") -ForegroundColor Green
                                Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName
                                #Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName -Role Owner  #Promote Use become a owner of channel                          
                            }else{
                                 Write-Host("Addding "+$UserName+" To Team: $($TeamgroupName.DisplayName) and to channel: $ChannelName") -ForegroundColor Green
                                 Add-TeamUser -GroupId $teamgroupid -User $UserName
                                 Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName
                            }
                            #Write-Host("I found thats channel name is: "+$ChannelName+" in team "+$TeamgroupName.DisplayName+" with teams groupid is: "+$teamgroupid)
                            #Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName -Role Owner  #Promote Use become a owner of channel                          
                          }

                }
            }
        }

    }
}

I suspect the problem is not so much with the script but with teams itself.
I believe that the Teams-service is not given enough time to register the user in the Team before the next cmdlet tries to add the user to a channel in the Team.
It may be worth trying to add a Start-Sleep -Seconds 15 (or some other value) between Add-TeamUser and Add-TeamChannelUser to give Teams the chance to see the newly added user.