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

Solved TFS 1.0 onLook

Codinablack

Dreamer
Content Editor
Joined
Dec 26, 2013
Messages
1,665
Solutions
11
Reaction score
928
Hello Otland. I have been trying to convert over @narko 's KDR for TFS 1.0 and been having some problems. I understand that onLook is done in events now and not creaturescripts. So I have tried altering my onLook function in players.lua file to look like this

Code:
function Player:onLook(thing, position, distance)
    local description = "You see " .. thing:getDescription(distance)
        function getKillsPlayer(cid)
            local Info = db.getResult("SELECT `frags` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. " LIMIT 1")
            local frags= Info:getDataInt("frags")
            return frags
        end

        function getDeathsPlayer(cid)
            local Info = db.getResult("SELECT `deaths` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. " LIMIT 1")
            local deaths = Info:getDataInt("deaths")
            return deaths
        end
    if self:getGroup():getAccess() then
        if thing:isItem() then
            description = string.format("%s\nItemID: [%d]", description, thing:getId())

            local actionId = thing:getActionId()
            if actionId ~= 0 then
                description = string.format("%s, ActionID: [%d]", description, actionId)
            end
           
            local uniqueId = thing:getAttribute(ITEM_ATTRIBUTE_UNIQUEID)
            if uniqueId > 0 and uniqueId < 65536 then
                description = string.format("%s, UniqueId: [%d]", description, uniqueId)
            end
           
            description = description .. "."
            local itemType = thing:getType()
           
            local transformEquipId = itemType:getTransformEquipId()
            local transformDeEquipId = itemType:getTransformDeEquipId()
            if transformEquipId ~= 0 then
                description = string.format("%s\nTransformTo: [%d] (onEquip).", description, transformEquipId)
            elseif transformDeEquipId ~= 0 then
                description = string.format("%s\nTransformTo: [%d] (onDeEquip).", description, transformDeEquipId)
            end

            local decayId = itemType:getDecayId()
            if decayId ~= -1 then
                description = string.format("%s\nDecayTo: [%d]", description, decayId)
            end
        elseif thing:isCreature() then
            if isPlayer(thing.uid) then
  if(getDeathsPlayer(cid)==0)then 
          kdr = getKillsPlayer(cid) 
    else 
    if(getDeathsPlayer(cid)~=0) then
    kdr = getKillsPlayer(thing.uid)/getDeathsPlayer(thing.uid)
    end
    end
 doPlayerSetSpecialDescription(thing.uid, (getPlayerSex(thing.uid) == 0 and "\nShe" or "\nHe") .. " has Killed: ["..getKillsPlayer(thing.uid).."] Players."..(getPlayerSex(thing.uid) == 0 and "\nShe" or "\nHe") .. " has Died: ["..getDeathsPlayer(thing.uid).."] Times."..(getPlayerSex(thing.uid) == 0 and "\nHer" or "\nHis") .. " Kill Death Ratio is: ["..kdr.."].")
 end
 if(thing.uid == cid) then
  if(getDeathsPlayer(cid)==0)then 
          kdr = getKillsPlayer(cid) 
    else 
    if(getDeathsPlayer(cid)~=0) then
    kdr = getKillsPlayer(thing.uid)/getDeathsPlayer(thing.uid)
    end
 doPlayerSetSpecialDescription(thing.uid, "\nYou have Killed: ["..getKillsPlayer(thing.uid).."] Players.\nYou have Died: ["..getDeathsPlayer(thing.uid).."] Times.\nYour Kill Death Ratio is: ["..kdr.."].")
 end
 return true
 end
            local str = "%s\nHealth: [%d / %d]"
            if thing:getMaxMana() > 0 then
                str = string.format("%s, Mana: [%d / %d]", str, thing:getMana(), thing:getMaxMana())
            end
            description = string.format(str, description, thing:getHealth(), thing:getMaxHealth()) .. "."
        end
       
        local position = thing:getPosition()
        description = string.format(
            "%s\nPosition: [X: %d] [Y: %d] [Z: %d].",
            description, position.x, position.y, position.z
        )
       
       
        --  if thing:isCreature() then
        ---    if thing:isPlayer() then
        ---        description = string.format("%s\nIP: [%s].", description, Game.convertIpToString(thing:getIp()))
        ---    end
        --- end
    end
    self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end

and I get this error

Code:
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:Player@onLook
data/events/scripts/player.lua:14: attempt to concatenate a boolean value
stack traceback:
        [C]: ?
        data/events/scripts/player.lua:14: in function 'getDeathsPlayer'
        data/events/scripts/player.lua:59: in function <data/events/scripts/play
er.lua:5>

The error only happens if I am on GM Character which is already one prolbem because I was trying for my bit of code to work for all players not just GM... second is obviously its not working because of a problem in the getDeathsPlayer function but doesn't seem to have a problem with the getKillsPlayer function.... Any help guys? Yes my database has deaths and frags and I have tried with characters set up with 0 3 4 and 2 deaths, all with 4 or more frags....
 
Code:
local player_thing = Player(thing.uid)

if player_thing ~= nil then --true = it's a player.
end

same effect as isPlayer(thing.uid)

Here is isPlayer function:

Code:
function isPlayer(cid) return Player(cid) ~= nil end

About your error, it really caught my attention. I tried to do the same here, but it turns out that every time I use look on a creature, the function is not called. I'll search more about it, I'm curious now. lol

edit:

When looking in creatures (including players) use onLookInBattleList(), it's the one that's called.

edit2:

I was trying to make your script work to help you more, but there's one thing that prevents me from doing so, my db don't have frags nor deaths in table players. Check if you do.
 
Last edited:
Yes mine does I used this to to put it in there....

Code:
ALTER TABLE `players` ADD `frags` INT( 11 ) NOT NULL DEFAULT '0';
ALTER TABLE `players` ADD `deaths` INT( 11 ) NOT NULL DEFAULT '0';
 
Ok, then it's all good. try this:

Code:
function getKillsPlayer(cid)
  local Info = db.storeQuery("SELECT `frags` FROM `players` WHERE `id` = " .. cid:getGuid() .. " LIMIT 1")
  local frags = db.escapeString(Info)

   return frags
end

function getDeathsPlayer(cid)
   local Info = db.storeQuery("SELECT `deaths` FROM `players` WHERE `id` = " .. cid:getGuid() .. " LIMIT 1")
   local deaths = db.escapeString(Info)

   return deaths
end
function Player:onLookInBattleList(creature, distance)
   local description = "You see " .. creature:getDescription(distance)
  
   description = string.format("%s\nThis player killed %d and died %d times", description, getKillsPlayer(creature), getDeathsPlayer(creature))

   if self:getGroup():getAccess() then
     local str = "%s\nHealth: [%d / %d]"
     if creature:getMaxMana() > 0 then
       str = string.format("%s, Mana: [%d / %d]", str, creature:getMana(), creature:getMaxMana())
     end
     description = string.format(str, description, creature:getHealth(), creature:getMaxHealth()) .. "."

     local position = creature:getPosition()
     description = string.format(
       "%s\nPosition: [X: %d] [Y: %d] [Z: %d].",
       description, position.x, position.y, position.z
     )

     if creature:isPlayer() then
       description = string.format("%s\nIP: [%s].", description, Game.convertIpToString(creature:getIp()))
     end
   end
   self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end

I believe it'll work, but I never used escapeString, so just debbug that part before trying for real and it should be fine.
 
Last edited:
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onLookInBattleList
data/events/scripts/player.lua:109: attempt to index global 'getKillsPlayercreat
ure' (a nil value)
stack traceback:
[C]: ?
data/events/scripts/player.lua:109: in function <data/events/scripts/pla
yer.lua:106>
 
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onLookInBattleList
data/events/scripts/player.lua:108: bad argument #3 to 'format' (number expected
, got string)
stack traceback:
[C]: ?
[C]: in function 'format'
data/events/scripts/player.lua:108: in function <data/events/scripts/pla
yer.lua:105>
 
Change %d to %s in
Code:
description = string.format("%s\nThis player killed %d and died %d times", description, getKillsPlayer(creature), getDeathsPlayer(creature))
 
Thank you very much!!!!!!!!

It sucks I have to do it thru the battle window but atleast it is working right now!!

It works for players but not for GM's because I have this error...

Code:
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:Player@onLookInBattleList
data/global.lua:257: attempt to index global 'bit' (a nil value)
stack traceback:
        [C]: ?
        data/global.lua:257: in function 'convertIpToString'
        data/events/scripts/player.lua:124: in function <data/events/scripts/pla
yer.lua:105>
[CODE]

I have that commented out for the looking at a player with shift+click, but I would like to know why it is messed up....


EDIT
The number goes up for both the death and the kills just by looking...

Code:
This player killed '39' and died '40' times
19:39 You see Chase. He is Administrator.
This player killed '41' and died '42' times
19:39 You see Chase. He is Administrator.
This player killed '43' and died '44' times
19:39 You see Chase. He is Administrator.
This player killed '45' and died '46' times
19:39 You see Chase. He is Administrator.
This player killed '47' and died '48' times
19:39 You see Chase. He is Administrator.
This player killed '49' and died '50' times
19:39 You see Chase. He is Administrator.
This player killed '51' and died '52' times
19:39 You see Chase. He is Administrator.
This player killed '53' and died '54' times
19:39 You see Chase. He is Administrator.
This player killed '55' and died '56' times
19:39 You see Chase. He is Administrator.
This player killed '57' and died '58' times
19:39 You see Chase. He is Administrator.
This player killed '59' and died '60' times
19:39 You see Chase. He is Administrator.
This player killed '61' and died '62' times
19:39 You see Chase. He is Administrator.
This player killed '63' and died '64' times
 
Ok, I now understand how it all works. I learnt a lot trying to help you, lol.

So, when we do db.storeQuery we are storing the result somewhere, so we need to do a result.getDataInt(place) to have the exact value. I'm trying to understand a warning I'm receiving but now it should all work:

Code:
function getKillsPlayer(cid)
   local Info = db.storeQuery("SELECT `frags` FROM `players` WHERE `id` = " .. cid:getGuid() .. " LIMIT 1")
   local frags = result.getDataInt(Info)

   return frags
end

function getDeathsPlayer(cid)
  local Info = db.storeQuery("SELECT `deaths` FROM `players` WHERE `id` = " .. cid:getGuid() .. " LIMIT 1")
  local deaths = result.getDataInt(Info)

  return deaths
end

function Player:onLookInBattleList(creature, distance)
  local description = "You see " .. creature:getDescription(distance)

  description = string.format("%s\nThis player killed %s and died %s times", description, getKillsPlayer(creature), getDeathsPlayer(creature))

  if self:getGroup():getAccess() then
  local str = "%s\nHealth: [%d / %d]"
  if creature:getMaxMana() > 0 then
  str = string.format("%s, Mana: [%d / %d]", str, creature:getMana(), creature:getMaxMana())
  end
  description = string.format(str, description, creature:getHealth(), creature:getMaxHealth()) .. "."

  local position = creature:getPosition()
  description = string.format(
  "%s\nPosition: [X: %d] [Y: %d] [Z: %d].",
  description, position.x, position.y, position.z
  )

  if creature:isPlayer() then
  description = string.format("%s\nIP: [%s].", description, Game.convertIpToString(creature:getIp()))
  end
  end
  self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end

about your error when using it with GM it's because of bit functions that my lua have and it seems you don't.

Edit:
This is getting more and more fun for me, PM me so I can add you skype and we can discuss about this script. It still doesn't work but I'll be glad to make it work :)

Edit2:

We did it :)

result.getDataInt need two arguments, one for the ID of the result and another for the colunm, so it's like result.getDataInt(Info, 'frags')

Code:
function getKillsPlayer(cid)
   local Info = db.storeQuery("SELECT `frags` FROM `players` WHERE `id` = " .. cid:getGuid())
   local frags = result.getDataInt(Info, 'frags')
   result.free(Info)


   return frags
end

function getDeathsPlayer(cid)
   local Info = db.storeQuery("SELECT `deaths` FROM `players` WHERE `id` = " .. cid:getGuid())
   local deaths = result.getDataInt(Info, 'deaths')
   result.free(Info)

   return deaths
end

function Player:onLookInBattleList(creature, distance)
  local description = "You see " .. creature:getDescription(distance)

  description = string.format("%s\nThis player killed %s and died %s times", description, getKillsPlayer(creature), getDeathsPlayer(creature))

  if self:getGroup():getAccess() then
  local str = "%s\nHealth: [%d / %d]"
  if creature:getMaxMana() > 0 then
  str = string.format("%s, Mana: [%d / %d]", str, creature:getMana(), creature:getMaxMana())
  end
  description = string.format(str, description, creature:getHealth(), creature:getMaxHealth()) .. "."

  local position = creature:getPosition()
  description = string.format(
  "%s\nPosition: [X: %d] [Y: %d] [Z: %d].",
  description, position.x, position.y, position.z
  )

  if creature:isPlayer() then
  description = string.format("%s\nIP: [%s].", description, Game.convertIpToString(creature:getIp()))
  end
  end
  self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end
 
Last edited:
Back
Top