Lineage 2 Tower Forum

Full Version: OnChatUserMessage: crashes if multiple messages appear
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I created a little script that checks whether someone wrote on the chat. When I use it, it sometimes works, but I noticed that if several messages appear, the plugin crashes.

Code:
function OnChatUserMessage(chatTypechatType, nick, msg)
    if detection1 then
if GetUserByName(nick):IsPlayer() then
PlaySound(GetDir() .. "silent_beep.wav");
ShowToClient("PM Guard1", "///.");
end
    end
    if detection2 then
if GetUserByName(nick):IsPlayer() and chatType == 2 then
PlaySound(GetDir() .. "silent_beep.wav");
ShowToClient("PM Guard2", "///.");
end
    end
end;
detection1 and detection2 are two booleans that control whether I want to check for all messages or only for whispers. ShowToClient is used here just for debugging.
Add Check if GetUserByName(nick) is not nil
Code:
function OnChatUserMessage(chatTypechatType, nick, msg)
    if detection1 then
        if GetUserByName(nick):IsPlayer() and GetUserByName(nick) ~= nil then
            PlaySound(GetDir() .. "silent_beep.wav");
            ShowToClient("PM Guard1", "///.");
        end
    end
    if detection2 then
        if GetUserByName(nick):IsPlayer() and chatType == 2 and GetUserByName(nick) ~= nil then
            PlaySound(GetDir() .. "silent_beep.wav");
            ShowToClient("PM Guard2", "///.");
        end
    end
end;
Works if I write something on the chat. I waited for a minute and two hero messages appeared. The plugin crashed, displayed yellow in the plugins list.
if GetUserByName(nick):IsPlayer() and GetUserByName(nick) ~= nil then

ohh god why... it should be:

if GetUserByName(nick) ~= nil and GetUserByName(nick):IsPlayer() then
Sorry, I am not so good at Lua language. What does it actually do if you write GetUserByName(nick) ~= nil? Also, is there a difference in which order you write if statement's conditions?

Seems to work now.
It will First check if the player "exist", if yes it will continue doing its job
Reference URL's