Not the best use case for Lua, but something like this might work:
Code:
local function OnCreatureTakeDamage(event, creature, player, damage)
local experienceReward = math.random(damage / 2, damage)
player:GiveXP(experienceReward)
end
local function OnPlayerEnterCombat(event, player, enemy, damage)
if not enemy or not player then
return
end
if not enemy:GetData("event") then
local id = RegisterUniqueCreatureEvent(enemy:GetGUID(), enemy:GetInstanceId(), 9, OnCreatureTakeDamage)
enemy:SetData("event", id)
end
end
RegisterPlayerEvent(33, OnPlayerEnterCombat)