This one takes a slightly different approach and lets you add/remove players with slash commands.
Lua Code:
local DeathCounter,DeathData,PlayerGUID=CreateFrame("Frame"),{},UnitGUID("player");
DeathCounter:SetScript("OnEvent",function(self,event,unit)
if event=="GROUP_ROSTER_UPDATE" then
if IsInGroup() and not IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then-- Check for party/raid, but not battleground
for guid,data in pairs(DeathData) do-- Remove members when they leave
if guid~=PlayerGUID and not IsGUIDInGroup(guid) then
DeathData[guid]=nil;-- Cleanup if no longer in group
print(("%s was removed from watch list."):format(data.Name));
end
end
if not next(DeathData) then self:UnregisterAllEvents(); end-- Disable if empty
else-- No longer in a valid group
table.wipe(DeathData);-- Clear all raid members
self:UnregisterAllEvents();-- Disable
print("Watch list cleared.");
end
elseif event=="UNIT_HEALTH" then
local guid,dead=UnitGUID(unit),UnitIsDead(unit);
local data=DeathData[guid]; if data then
if dead and not data.WasDead then-- TLDR: Did unit just die?
data.Deaths=data.Deaths+1;-- Increment deaths
if not IsInRaid() or UnitIsGroupLeader("player") or UnitIsGroupAssistant("player") then-- Can't send RAID_WARNING if not either of these
SendChatMessage(("DeathCounter: %s has died %d time(s)."):format(UnitName(unit),data.Deaths),"RAID_WARNING");
end
end
data.WasDead=dead;-- Update living state
end
end
end);
function SlashCmdList.DEATHCOUNTER_ADDUNIT(unit)
-- Test Group
if not IsInGroup() then print("You must be in a party/raid to use this command."); return; end
if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then print("You cannot use this command in a battleground."); return; end
-- Test Unit
if (unit or "")=="" then unit="target"; end
if not (UnitIsPlayer(unit) and (UnitInParty(unit) or UnitInRaid(unit))) then print("Unit must be a player in your party/raid."); return; end
if IsInRaid() and not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then
print("Warning: Raid warnings can only be sent by a raid leader or assistant.\nDeaths will still be counted.");
end
local name,guid=UnitName(unit),UnitGUID(unit);
if not DeathData[guid] then
DeathData[guid]={Name=name,WasDead=UnitIsDead(unit),Deaths=0};-- Add unit
DeathCounter:RegisterEvent("GROUP_ROSTER_UPDATE");-- Register group changes
DeathCounter:RegisterEvent("UNIT_HEALTH");-- Enable death watch
print(("Now watching %s."):format(name));
else print(("Already watching %s."):format(name)); end
end
SLASH_DEATHCOUNTER_ADDUNIT1="/dcadd";
function SlashCmdList.DEATHCOUNTER_REMUNIT(unit)
if (unit or "")=="" then unit="target"; end
if not next(DeathData) then print("Watch list is empty."); return; end
if not UnitIsPlayer(unit) then print("Unit must be a player."); return; end
local name,guid=UnitName(unit),UnitGUID(unit);
if DeathData[guid] then
DeathData[guid]=nil;-- Remove entry
if not next(DeathData) then DeathCounter:UnregisterAllEvents(); end-- Disable if empty
print(("%s was removed from watch list."):format(name));
else print(("%s not found in watch list."):format(name)); end
end
SLASH_DEATHCOUNTER_REMUNIT1="/dcrem";
function SlashCmdList.DEATHCOUNTER_CLEAR()
if next(DeathData) then
table.wipe(DeathData);-- Clear
DeathCounter:UnregisterAllEvents();-- Disable
print("Watch list cleared.");
else print("Watch list is empty."); end
end
SLASH_DEATHCOUNTER_CLEAR1="/dcclr";
Usage:
/dcadd unit - Adds
unit to list of players being watched.
/dcrem unit - Removes
unit from list of players being watched.
/dcclr - Clears watch list.
Notes: Only players in your party/raid can be added to the watch list. They will be automatically removed if they leave the group. The list is also cleared if you leave the group. Even though these commands are written to take a UnitID, keep in mind the names of players in your party/raid are also valid UnitIDs.