How to stop players from killing specific mobs in minecraft?

I'm using a few commands (on repeat + always active) where it summons zombies with customnames (1, 2 and 3) and also have 3 teams, also called "1" "2" and "3". At the start of the map, one player joins team 1 and the other joins team 2. I am also doing team (1 and 2 individually) friendlyfire false, and also have commands that zombies join the team of their respective names.

My expected result: if I'm a player in team 1, I can only hurt zombies from teams 2 and 3, but not from team 1

Actual result: regardless of which team I'm in, I can still kill zombies on the same team

Is it possible to truly disable the killing of certain mobs by specific players (so teamwork is required to beat them) rather than having both players able to fight all mobs?

I'm using all the commands wiki shows me, team making, team joining and team disabling friendly fire (which should be enough to do the trick but appearently doesn't work).


Solution 1:

friendlyFire: Enable/Disable players inflicting damage on each other when on the same team. (Note: players can still inflict status effects on each other.) Does not affect some non-player entities in a team.

The reason disabling friendly fire doesn't work is because the zombies count as non-player entities.

Unfortunately, there is no easy solution to this problem.

You could give all zombies a high level of resistance, making them hittable but not damageable. Whenever a zombie is hit, have all players raycast to detect if they are pointing at the hurt zombie. If they are on a different team, artificially apply the damage.