It won't work exactly like that. This, theoretically, is what you want to use. You might have to tweak it but I think it's coded up the way you need it to be.
Lua Code:
local addonName, addon = ...
local llRI = nil
local npcToWatch = 205089
local function OnEvent(self,event,...)
if event == "PLAYER_TARGET_CHANGED" then
local guid = UnitGUID("target")
if guid == nil then return end
local guidType, _, _, _, _, npc_id, _ = strsplit("-",guid)
if guidType == "Creature" and tonumber(npc_id) == npcToWatch then
if GetRaidTargetIndex("target") == nil then
if llRI == nil then
llRI = 0
end
llRI = (llRI % 8) + 1
if GetRaidTargetIndex("target") ~= llRI then
SetRaidTarget("target", llRI)
PlaySound(SOUNDKIT.GM_CHAT_WARNING)
end
end
end
end
end
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_TARGET_CHANGED")
f:SetScript("OnEvent",OnEvent)
UnitGUID("target") will only return a value if you have physically targetted a mob - I mentioned PLAYER_TARGET_CHANGED event being the one to watch for that happening to do your work.
Unless you are only ever going to need that one single creature then you will have to edit your addon, reload your UI so you can do it again for the next target of interest. And when they rebuild the GUIDs you will likely have to change the numbers again. It depends on whether these are special entities that keep their GUID permanently like players.
However, you could add in a slash command that allows you to type in the id of the next npc if you will need to repeat this for each npc you will be going after and to cater for GUID changes. I will leave that task up to you, but all you will need to do in that block of code is grab the id you typed and set that to the variable set up for it.