Thanks for your answer.
Here is the original Gello version
-- To use: /click TargetRandomGroupMember
-- To be more selective on who to target, update the function isTargetable()
-- return true if the given unit ("raid22","party3") should be a candidate for targeting
local function isTargetable(unit)
return true -- change to return UnitClass(unit)=="Mage" to target only random mages
end
-- generates a list of units as attributes randomUnit1, randomUnit2, ..., randomUnit[randomNumUnits]
-- that are the units to target, such as randomUnit1="raid3", randomUnit2="raid14", etc
local function updateUnits(button)
-- can't set unit attributes in combat
if InCombatLockdown() then
return
end
button:SetAttribute("randomNumUnits",0)
-- start building list of units again
local numUnits = 0
local groupType = IsInRaid() and "raid" or "party"
for i=1,groupType=="raid" and 40 or 5 do
local unit = (groupType=="party" and i==5) and "player" or groupType..i
if UnitExists(unit) and isTargetable(unit) then
numUnits = numUnits + 1
button:SetAttribute("randomUnit"..numUnits,unit)
end
end
button:SetAttribute("randomNumUnits",numUnits)
end
-- /click TargetRandomGroupMember will click this button that's never made visible
local button = CreateFrame("Button", "TargetRandomGroupMember", UIParent, "SecureActionButtonTemplate")
-- set up events so updateUnits is called when logging in or the group changes
button:RegisterEvent("PLAYER_LOGIN")
button:RegisterEvent("GROUP_ROSTER_UPDATE")
button:SetScript("OnEvent",updateUnits)
-- the /click will target a to-be-chosen unit (chosen in the pre-click wrap below)
button:SetAttribute("type","target")
-- set up pre-click wrap to pick a random unit (or the player if no candidates)
SecureHandlerWrapScript(button,"OnClick",button,[[
local numUnits = self:GetAttribute("randomNumUnits") or 0
self:SetAttribute("unit",numUnits>0 and self:GetAttribute("randomUnit"..random(numUnits)) or nil)
]])
And here is the updated version for PI (i would need almost the same with Envelopping mist). Still based on Gello 2020 code.
--[[
source: modified from https://us.forums.blizzard.com/en/wow/t/select-random-friendly-target/482404/6
Paste code to https://addon.bool.no to turn it into an addon
To use: /click CastPI
--]]
local function isTargetable(unit)
return (UnitGroupRolesAssigned(unit)=="DAMAGER") and (UnitClass(unit)~="Priest")
end
local function updateUnits(button)
if InCombatLockdown() then
return
end
button:SetAttribute("randomNumUnits",0)
-- start building list of units again
local numUnits = 0
local groupType = IsInRaid() and "raid" or "party"
for i=1,groupType=="raid" and 40 or 5 do
local unit = (groupType=="party" and i==5) and "player" or groupType..i
if UnitExists(unit) and isTargetable(unit) then
numUnits = numUnits + 1
button:SetAttribute("randomUnit"..numUnits,unit)
end
end
button:SetAttribute("randomNumUnits",numUnits)
end
local button = CreateFrame("Button", "CastPI", UIParent, "SecureActionButtonTemplate")
button:RegisterEvent("PLAYER_LOGIN")
button:RegisterEvent("GROUP_ROSTER_UPDATE")
button:SetScript("OnEvent",updateUnits)
button:SetAttribute("type","spell")
button:SetAttribute("spell","Power Infusion")
-- set up pre-click wrap to pick a random unit (or the player if no candidates)
SecureHandlerWrapScript(button,"OnClick",button,[[
local numUnits = self:GetAttribute("randomNumUnits") or 0
self:SetAttribute("unit",numUnits>0 and self:GetAttribute("randomUnit"..random(numUnits)) or "player")
]])
Thanks