• 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!

[TFS 1.0][LUA] getResult returns a nil value

Matti96

Abyss
Joined
Apr 17, 2009
Messages
254
Reaction score
46
Location
Poland/Germany
Hi :) I'm new to scripting with SQL. So I made this script to check if a player with a specific name exists in the datbase.

The function:
local function getPlayerExists(pname)
local result = db.getResult("SELECT `name` FROM `players` WHERE `pname` = " .. db.escapeString(pname))
if(result:getID() == -1) then
return false
end
local value = result:getDataString("name")
result:free()
return value
end

But I'm getting this error:
Lua Script Error: [Npc interface]
Lua Script Error: [Npc interface]
data/npc/scripts/Thanata/Naji.lua:eek:nCreatureSay
data/npc/scripts/Thanata/Naji.lua:88: attempt to call field 'getResult' (a nil value)
stack traceback:
[C]: in function 'getResult'
data/npc/scripts/Thanata/Naji.lua:88: in function 'getPlayerExists'
data/npc/scripts/Thanata/Naji.lua:311: in function 'callback'
data/npc/lib/npcsystem/npchandler.lua:404: in function 'onCreatureSay'
data/npc/scripts/Thanata/Naji.lua:20: in function <data/npc/scripts/Thanata/Naji.lua:20>

Thanks!
 
Change this: local result = db.getResult, to
Code:
local resultx = db.storeQuery

This: if(result:getID() == -1) then, to
Code:
if not resultx then

This: local value = result:getDataString("name"), to
Code:
local value = result.getDataString(resultx, 'name')

And this: result:free(), to
Code:
result.free(resultx)

The change of the variable result is because it else will conflict with result.getDataString etc, but you don't have to call it resultx, just something else than result.
 
Change this: local result = db.getResult, to
Code:
local resultx = db.storeQuery

This: if(result:getID() == -1) then, to
Code:
if not resultx then

This: local value = result:getDataString("name"), to
Code:
local value = result.getDataString(resultx, 'name')

And this: result:free(), to
Code:
result.free(resultx)

The change of the variable result is because it else will conflict with result.getDataString etc, but you don't have to call it resultx, just something else than result.

Thank you! even though the name exists in the db it says that it doent exist...

if getPlayerExists(transfer[cid]) then
selfSay("So you would like to transfer " .. count[cid] .. " gold to \"" .. transfer[cid] .. "\" ?", cid)
talkState[cid] = 13
else
selfSay("Player with name \"" .. transfer[cid] .. "\" doesnt exist.", cid)
talkState[cid] = 0
end

Error:
Lua Script Error: [Npc interface]
data/npc/scripts/Thanata/Naji.lua:eek:nCreatureSay
data/npc/scripts/Thanata/Naji.lua:75: attempt to compare number with boolean
stack traceback:
[C]: in function '__lt'
data/npc/scripts/Thanata/Naji.lua:75: in function 'doPlayerTransferMoneyTo'
data/npc/scripts/Thanata/Naji.lua:320: in function 'callback'
data/npc/lib/npcsystem/npchandler.lua:404: in function 'onCreatureSay'
data/npc/scripts/Thanata/Naji.lua:20: in function <data/npc/scripts/Thanata/Naji.lua:20>

Function
local function doPlayerTransferMoneyTo(cid, target, amount)
local balance = getPlayerBalance(cid)
if(amount > balance) then
return false
end
local tid = getPlayerByName(target) -- LINE 75
if(tid > 0) then
doPlayerSetBalance(tid, getPlayerBalance(tid) + amount)
else
if(getPlayerExists(target) == FALSE) then
return false
end
db.executeQuery("UPDATE `player_storage` SET `value` = `value` + '" .. amount .. "' WHERE `player_id` = (SELECT `id` FROM `players` WHERE `name` = '" .. escapeString(player) .. "') AND `key` = '" .. balance_storage .. "'")
end
doPlayerSetBalance(cid, getPlayerBalance(cid) - amount)
return true
end
 
Back
Top