Team command doesn't work in functions

I'm trying to make a function, that when run, swaps the teams of two armor stands. I have this code written in a function on a repeating command block to set up the swap function, and the "less_num1" is the function that is supposed to swap them.

Set-up code:

execute if score @e[type=minecraft:armor_stand,team=ph,limit=1] time < @e[type=minecraft:armor_stand,team=num1,limit=1] time run function minigames:less_num1

This somewhat works, and it executes the function when it's supposed to, but the swap function that I wrote doesn't seem to work at all.

Swap Function:

execute as @e[type=minecraft:armor_stand,team=num1,limit=1,sort=nearest] run 
team join ph @s
execute as @e[type=minecraft:armor_stand,team=ph,limit=1,sort=nearest] run 
team join num1 @s

I have tried multiple variations of this and they don't seem to work either. When I run both of these commands by hand, they work, but they don't work in the function. Does anyone have a fix for this?


You're putting everyone from one team into another team and then from that other team back into the first team. The result is that everyone ends up in the first team.

You need to put everyone from team A into a temporary team C, then everyone from team B Into team A, then everyone from team C into team B.