• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua getPlayerGUIDByName dosn't work?

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
[15/08/2016 17:45:39] [Error - Npc interface]
[15/08/2016 17:45:39] data/npc/scripts/bank.lua:eek:nCreatureSay
[15/08/2016 17:45:39] Description:
[15/08/2016 17:45:39] data/npc/lib/npc.lua:54: attempt to index local 'keyword' (a number value)
[15/08/2016 17:45:39] stack traceback:
[15/08/2016 17:45:39] data/npc/lib/npc.lua:54: in function 'msgcontains'
[15/08/2016 17:45:39] data/npc/scripts/bankerrothschild.lua:135: in function 'callback'
[15/08/2016 17:45:39] data/npc/lib/npcsystem/npchandler.lua:390: in function 'onCreatureSay'
[15/08/2016 17:45:39] data/npc/scripts/bankerrothschild.lua:52: in function <data/npc/scripts/bank.lua:52>

Why this code throws the error above? The error appears instead "
msg..''does not exist'" only when the code can't find the player.
Code:
if msgcontains(msg, selectPlayerName(msg)) then
        
            if getPlayerGUIDByName(selectPlayerName(msg)) ~= 0 then
                selfSay('Nick exists: '..msg, cid)
            else
                selfSay(msg..' does not exist.', cid)
            end

This is a function i use:
Code:
function selectPlayerName(name)
    local result = db.getResult("SELECT `name` FROM `players` WHERE `name` = "..db.escapeString(name))
    if (result:getID() ~= -1) then
        local return_name = result:getDataString('name')
        result:free()
        return return_name
    end
    return 0
end
 
There is so many problems with what you are trying to do....

To start... This will not work at all.

Code:
if msgcontains(msg, selectPlayerName(msg)) then
 
I would like to make a script, which checks if a player exist in database or not. This script has to be used inside a creatureSayCallback(cid, type, msg) as a msgcontains(msg, 'playername') function.
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local talkState = {}

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end

function creatureSayCallback(cid, type, msg) local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    if not npcHandler:isFocused(cid) then
        if msg == 'hi' then
            selfSay('tell player name', cid)
            npcHandler:addFocus(cid)
        else
            return false
        end
    else
        if msgcontains(msg, '') then -- here player name
            selfSay('this player exists in database: '..msg, cid)
        end
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
If the player does not exist, script should say that player does not exist.
Some functions can be usefull:
Code:
        getCreatureByName(name)
        getCreatureName(cid)
        getPlayerGUIDByName(name) ~= 0
        getPlayerByNameWildcard

I saw a script like that in a bank transfer script, but it wasn't working for else too.
This person had the same problem like me, but the links are deleted: https://otland.net/threads/naji-bank-npc-error-transfer-gold.206953/

I've tried this way and it works, but i want to write a name on my own and check if it does not exist:
Code:
        if msgcontains(msg, 'waqmaz') then
            local v = getPlayerByName(msg)
            if v then
                selfSay('name: '..getPlayerName(v), cid)
            end
        end
I've tried this code, but it returns error, when the player does not exist:
data/npc/lib/npc.lua:54: attempt to index local 'keyword' (a nil value)
Code:
        local function findPlayer(name)
    local q = db.getResult('SELECT name FROM players WHERE name=' .. db.escapeString(name) .. ' LIMIT 1'), nil
    if q:getID() == -1 then
        return
    end
    local r = q:getDataString('name')
    q:free()
    return r
end

if msgcontains(msg, findPlayer(msg)) then 
            print('name: '..findPlayer(msg))
        end

Looks like I found why there is an error. It's about a ":lower" in data/npc/lib/npc.lua:54. 54 line is this:
Code:
function doMessageCheck(message, keyword)
    if(type(keyword) == "table") then
        return table.isStrIn(keyword, message)
    else
        print('1. hello its an error :D')
    end

    local a, b = message:lower():find(keyword:lower())
   
    if(a ~= nil and b ~= nil) then
        return true
    else
        print('2. hello its an error :D')
    end

    return false
end
I've changed i to:
Code:
local a, b = message:find(keyword)
And now it works. Now I have a question, how to add a lower to the function findPlayer, ebcause I do not want to delete a lower from npc.lua.
 
Last edited:
Example code. Should work.
Code:
local player_name = msg
if getPlayerGUIDByName(player_name) ~= 0 then
    selfSay("Player " .. player_name .. " exists in database. Player GUID is | " .. getPlayerGUIDByName(player_name) .. " |.", cid)
else
    selfSay("Player " .. player_name .. " does not exist in database.", cid)
end

-- Edit

To clarify, you can hide this behind a talkState check if you need to.
 
Last edited:
Yeah, I have written the same code before.
I the player doesnt exist it returns error:
Code:
[16/08/2016 09:01:49] [Error - Npc interface]
[16/08/2016 09:01:49] data/npc/scripts/bankerrothschild.lua:onCreatureSay
[16/08/2016 09:01:49] Description:
[16/08/2016 09:01:49] data/npc/lib/npc.lua:54: attempt to index local 'keyword' (a number value)
[16/08/2016 09:01:49] stack traceback:
[16/08/2016 09:01:49]     data/npc/lib/npc.lua:54: in function 'msgcontains'
[16/08/2016 09:01:49]     data/npc/scripts/bankerrothschild.lua:100: in function 'callback'
[16/08/2016 09:01:49]     data/npc/lib/npcsystem/npchandler.lua:390: in function 'onCreatureSay'
[16/08/2016 09:01:49]     data/npc/scripts/bankerrothschild.lua:15: in function <data/npc/scripts/bankerrothschild.lua:15>
The 54 line of a inbuild function of TFS 0.3.6 in a npc.lua file is the red one:
Code:
function doMessageCheck(message, keyword)
    if(type(keyword) == "table") then
        return table.isStrIn(keyword, message)
    end

    local a, b = message:lower():find(keyword:lower())

    if(a ~= nil and b ~= nil) then
        return true
    end

    return false
end
To make it work, you have to remove ":lower" from the line like:
Code:
local a, b = message:find(keyword)
But it causes a bug that we have to write everything using capital letter.
Instead of removing ":lower" from the doMessageCheck function, I would like to add ":lower" to our script. How to do that?
For now I have this script:
Code:
local function findPlayer(name)
    local q = db.getResult('SELECT name FROM players WHERE name=' .. db.escapeString(name) .. ' LIMIT 1'), nil
    if q:getID() == -1 then
        return q:getID()
    end
    local player_name = q:getDataString('name')
    q:free()
    return player_name
end
and this script:
Code:
        if msgcontains(msg, findPlayer(msg)) then
   
            local player_name = msg
            if getPlayerGUIDByName(player_name) ~= 0 then
                selfSay("Player " .. player_name .. " exists in database. Player GUID is | " .. getPlayerGUIDByName(player_name) .. " |.", cid)
            else
                selfSay("Player " .. player_name .. " does not exist in database.", cid)
            end
   
        end
Where should I add ":lower"? Thanks.
It looks like this now: https://imgur.com/a/pWHXr
And something looks bugged, because everything after "else" doesn't display:
This doesn't work:
Code:
 else
                selfSay("Player " .. player_name .. " does not exist in database.", cid)
            end
Damn, why does it always return [16/08/2016 09:01:49] data/npc/lib/npc.lua:54: attempt to index local 'keyword' (a number value) instead of else when playerGUID == 0 ? I do not understand that.

Seems that there is a problem inside a function:
Code:
local function findPlayer(name)
    local q = db.getResult('SELECT name FROM players WHERE name=' .. db.escapeString(name) .. ' LIMIT 1'), nil
    if q:getID() == -1 then
        return -- HERE IS A PROBLEM (it is returned when player doesnt exist) - i think
    end
    local player_name = q:getDataString('name')
    q:free()
    return player_name
end

I do not know, I've lost my mind. getPlayerGUIDByName doesn't work, while it returns -1 or 0.

pWHXr
 
Last edited:
I'll test when I get home and try to get something working for you.
I've done this before but can't remember where or if I posted it.
 
There is a problem with the function getPlayerGUIDByName.
I've checked it this way:
Code:
local function sayName(text)
    return text
end

if msgcontains(msg, sayName(msg)) then   

                local player_name = msg
                if getPlayerGUIDByName(player_name) ~= 0 then
                    selfSay("Player " .. player_name .. " exists in database. Player GUID is | " .. getPlayerGUIDByName(player_name) .. " |.", cid)
                else
                    selfSay("Player " .. player_name .. " does not exist in database.", cid)
                end
end
If player dosn't exist in database, then script instead of saying ""Player " .. player_name .. " does not exist in database." says:
[16/08/2016 12:07:36] [Error - Npc interface]
[16/08/2016 12:07:36] data/npc/scripts/bankerrothschild.lua:eek:nCreatureSay
[16/08/2016 12:07:36] Description:
[16/08/2016 12:07:36] data/npc/scripts/bankerrothschild.lua:113: attempt to concatenate a nil value
[16/08/2016 12:07:36] stack traceback:
[16/08/2016 12:07:36] data/npc/scripts/bankerrothschild.lua:113: in function 'callback'
[16/08/2016 12:07:36] data/npc/lib/npcsystem/npchandler.lua:390: in function 'onCreatureSay'
[16/08/2016 12:07:36] data/npc/scripts/bankerrothschild.lua:15: in function <data/npc/scripts/bankerrothschild.lua:15>
 
FIX:
nil instead of -1:
Code:
local function typeName(text)
   return text
end

if msgcontains(msg, typeName(msg))
             player = typeName(msg)
            if getPlayerGUIDByName(player) == nil then -- this C++ function returns a nil value, not a number value

                selfSay('Please write the player\'s name correctly. ', cid)
            else
                selfSay('Do you really want to join a guild of guildleader '..player..'?', cid)
            end
end
 
Last edited:
Sorry, it was working. I did something wrong and thought that it didn't work. But it worked.
ve'updated the post above
 
Back
Top