Lua Question regarding: Self, PlayerName

Pretty big noob when it comes to coding. A friend of mine was helping me out with a project that has since been abandoned, and now I’m in the process of picking up the pieces and working out some of its old kinks.

The following code is my current focus:

   function EmoteNonsense_CheckForNames(msg, sender)
    local enWasFound = 0;
    local enPlayerName, _ = UnitName("player")
    if enNames ~= nil then
        for i=1,#enNames do
            local currentName = enNames[i]
            if msg:find("%f[%a]" .. EmoteNonsense_NoCase(currentName) .. "%f[%A]") and enPlayerName ~= self then		
                EmoteNonsense_DoRaidWarning(sender)
                EmoteNonsense_DoFlashClient()
                enWasFound = 1;
                msg = "|cFF"..enPlayerColour..">>|r"..msg
				end
            end
        end
    end
    if enWasFound == 0 then
        if msg:find("%f[%a]" .. EmoteNonsense_NoCase(enPlayerName) .. "%f[%A]") then
            EmoteNonsense_DoRaidWarning(sender)
            EmoteNonsense_DoFlashClient()
            msg = "|cFF"..enPlayerColour..">>|r"..msg
       end
    end
    return msg
end

The intent of this method is to “ping” the user when their character name is mentioned in chat, whispers, etc. However, the method also pings a player when they mention themselves, and that is what I don’t want.

I’ve tried adding a second condition, enPlayerName ~= self but I don’t think I quite understand how to reference oneself, and thus this code does not work and will not ping at all regardless of who sends the message.

What would be the best way to say, “If the sender of the message is NOT me”?

Thanks for any help!

self is in essence a programming term for the entity that owns a thing like a function that is processing the code. It’s also passed to functions as a generic reference to an object.

In this case, it doesn’t apply. You would probably (and I say this loosely because I only have part of the code) do something more like.

if enPlayerName ~= currentName and msg:find("%f[%a]" .. EmoteNonsense_NoCase(currentName) .. "%f[%A]") then

Either that or do a second find replacing currentName with enPlayerName

Either way, enPlayerName is you (the “player”) that you should be comparing, not self.

1 Like

If sender is the name of whoever sent the message then you would want

enPlayerName ~= sender

Thanks for the advice so far, ladies and gents. Unfortunately none of the suggestions have yet worked. Quite frustrating, but I’mma keep on keeping on!

It’s a bit hard to tell what might be going on with just the one function and no context to work with.

I’ve actually decided to take out all of that stuff for now and work on an entirely different problem. The addon did multiple things, but only one of them is really needed these days, since other addons cover everything else.