How do I give half of a player's emeralds to their killer?

I solved this with a datapack: http://www.mediafire.com/file/x2njlcqr5qgdpwg/file

There are several parts to this datapack. The first, getting the amount of emeralds someone has:

execute as @a store result score @s emeralds run clear @s minecraft:emerald 0

This runs every tick and stores how many emeralds every player has in a scoreboard called emeralds.

Then, I need to detect a player getting killed (the victim) and the player that killed them (the killer). I can do this with 2 scoreboards; one for detecting deaths and one for detecting kills:

execute as @a[scores={deathCount=1},limit=1] run function...
execute as @a[scores={playerKillCount=1},limit=1] run function...

There is one downside: if 2 players are killed in the same tick, the killers/victims may get mixed up.

Once a victim is detected I reset the victim's death count, assign them a tag, and divide their emerald count in half:

scoreboard players reset @s deathCount
tag @s add victim
scoreboard players operation @s emeralds /= #2 emeralds 

Once a killer is detected I reset the killer's kill count and give them the right amount of emeralds:

scoreboard players reset @s playerkillCount

function emerald:give

The killer will run a recursive function that will give them an amount of emeralds equal to the victim's emerald score:

scoreboard players remove @a[tag=victim,limit=1] emeralds 1
give @s emerald 1
execute if score @a[tag=victim,limit=1] emeralds matches 1.. run function emerald:give

It decreases the victim's score, gives 1 emerald, and runs again if the victim's score is not 0. In this way, I can avoid hardcoding the amount of emeralds to give.

Finally, I clear the tags.

tag @a[tag=victim,limit=1] remove victim

To download the datapack, use the link and extract the zip file into the datapacks folder. I could not test this datapack due to not having friends, but it should work in theory.