Macro Target Random Friendly Player

Hello!
I’m searching for a macro/addon that allows me to randomly target a Friendly Player.
Why? Because i’m a MW Monk player and with the actual state of the class, Chi’ji ramps are played with such insane amount of Haste that you lose time bitween GCD’s when interracting with Raid Frame every 2/3 spells to put envelloping mists on friendly players.
I saw on Ui and Macro forums that some macro/addons really close to my needs already exists. I saw that Gello posted in 2020 a code that could do what i need. But after following all the steps (addon.bool.no, copying freshly created addon in wow/interface/Addons) it just doesn’t work in game.

I found on wago a « weakauras/custom addon » that allows priest to use PI on random raid players (developped on Gello code). I tried to adapt this with my envelopping mist but it does not work because i may have done mistakes since my knowledge about lua is really limited.

If anyone can help me about that, it would be really cool! i’m starting to despair.
Thanks for reading, dear Wowers.

You got a link to them? Wrap them in code tags, </> button, if you don’t have permission to post links.

1 Like

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 :wink:

Untested, but this should do what you want, assuming no API changes with the functions used by the code.
/click RandomTarget

If it triggers errors let me know what they are. If it doesn’t, but doesn’t work, change the macro to /click RandomTarget X 1

local function isTargetable(unit)
    return true
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", "RandomTarget", UIParent, "SecureActionButtonTemplate")

button:RegisterEvent("PLAYER_LOGIN")
button:RegisterEvent("GROUP_ROSTER_UPDATE")
button:SetScript("OnEvent",updateUnits)

button:SetAttribute("type","spell")
button:SetAttribute("spell","Enveloping Mist")

-- 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")
]])
1 Like

Hello! And thanks for your time!

I just tried in game and sadly it does not work :frowning:
I took the code you made and put it into addon.bool.no. Then i copied the newly created addon in Wow/retail/interface/AddOns. I see the Addon in game in my list but when i create both macro and use them it just does not do anything.
You have to know that my game is in french. But it should not be a thing since macros in general except for spell names are in english.

Just tested it on my Monk and /click RandomTarget is working for me, while not in a group and out of combat.

1 Like

Im trying to find what i could do wrong. Do you see any mistakes in the previous step i described in my message?
I’ll swap my game in english and test again

Edit: Just swaped in Eng and does not work. So i’v made a mistake using addon.bool.no or copying the files. But i don’t see where :frowning:

If you put /click RandomTarget into chat does it do anything?

Only part you should need to change for language is:

button:SetAttribute("spell","Enveloping Mist")

Also, try adjusting this CVar:

/console ActionButtonUseKeyDown 0

Having it set to 1 is where this comes in, but let’s rule that out entirely, for now.

1 Like

Should i replace X by my name IG? or leave X?
Edit: i’m not sure it’s randomizing the targeting but it cast Enveloping Mist on me. i’ll get a raid and try quickly

You’d leave it as X.

Holly mate!

/click RandomTarget X 1 does not work BUT /click RandomTarget is working perfectly!!!
That’s incredible

edit: must have been the Cvar

1 Like

So if you change the CVar back then the X version should work.

Could try

/click RandomTarget LeftButton 1

but X should work.

May also need to make this edit, cuz why not…

button:RegisterEvent("PLAYER_LOGIN")
button:RegisterEvent("GROUP_ROSTER_UPDATE")

to

button:RegisterEvent("PLAYER_LOGIN")
button:RegisterEvent("GROUP_ROSTER_UPDATE")
button:RegisterForClicks("AnyDown","AnyUp")
1 Like

Reversed the Cvar and /click RandomTarget X 1 works

Edit: Honestly that’s incredibly kind to take that time to help me.
I will make the edit and let you know. What does it do? (i’ll have to do some stuff IRL, but will be back in 2hours)

1 Like

If it’s working you don’t need to make the edit. My thought was that by default it was only registered for KeyUp, which is why it wasn’t working when you first tested.

1 Like

Incredible job Elvanbane. Thanks again!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.