• 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 onLook, how to get target cid?

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,529
Solutions
1
Reaction score
85
Location
Portugal
Hello, so I'm trying to make something that when you look at yourself or another player it will show they're health and mana stats, it works fine if I look at myself or another player but if I try to look at anything else I get boolean error in console because of line:

Lua:
local health = (getCreatureHealth(thing.uid) / 1000)

I assume cuz it's trying to get the stats from the tile or item that I'm looking at, so how do I stop that and make the script only show this if the creature i'm looking at is a player?

here's the full script:

Lua:
function onLook(cid, thing, position, lookDistance)
   local info = ""
   local health = (getCreatureHealth(thing.uid) / 1000)
   local maxhealth = (getCreatureMaxHealth(thing.uid) / 1000)
   local mana = (getPlayerMana(thing.uid) / 1000)
   local maxmana = (getPlayerMaxMana(thing.uid) / 1000)

   if isPlayer(thing.uid) then
       info = "".. getPlayerName(thing.uid) .." has ".. math.ceil(health) .."k/".. math.ceil(maxhealth) .."k health and ".. math.ceil(mana) .."k/".. math.ceil(maxmana) .."k mana!"
       doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, info)
       doCreatureSay(cid, info, TALKTYPE_MONSTER_SAY, false, cid, getThingPos(thing.uid))
   end
return true
end
 
Solution
Screenshot

They're all inside same table so you must use target instead of stats.target xd
I did test this script and it did work.
Lua:
function onLook(cid, thing, position, lookDistance)
    local target = getTopCreature(position).uid
    if isPlayer(target) then
        local info = ""
        local health = (getCreatureHealth(target) / 1000)
        local maxhealth = (getCreatureMaxHealth(target) / 1000)
        local mana = (getPlayerMana(target) / 1000)
        local maxmana = (getPlayerMaxMana(target) / 1000)
        info = "".. getPlayerName(target) .." has ".. math.ceil(health) .."k/".. math.ceil(maxhealth) .."k health and ".. math.ceil(mana) .."k/".. math.ceil(maxmana) .."k mana!"
        doPlayerSendTextMessage(cid...
Hello, so I'm trying to make something that when you look at yourself or another player it will show they're health and mana stats, it works fine if I look at myself or another player but if I try to look at anything else I get boolean error in console because of line:

Lua:
local health = (getCreatureHealth(thing.uid) / 1000)

I assume cuz it's trying to get the stats from the tile or item that I'm looking at, so how do I stop that and make the script only show this if the creature i'm looking at is a player?

here's the full script:

Lua:
function onLook(cid, thing, position, lookDistance)
   local info = ""
   local health = (getCreatureHealth(thing.uid) / 1000)
   local maxhealth = (getCreatureMaxHealth(thing.uid) / 1000)
   local mana = (getPlayerMana(thing.uid) / 1000)
   local maxmana = (getPlayerMaxMana(thing.uid) / 1000)

   if isPlayer(thing.uid) then
       info = "".. getPlayerName(thing.uid) .." has ".. math.ceil(health) .."k/".. math.ceil(maxhealth) .."k health and ".. math.ceil(mana) .."k/".. math.ceil(maxmana) .."k mana!"
       doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, info)
       doCreatureSay(cid, info, TALKTYPE_MONSTER_SAY, false, cid, getThingPos(thing.uid))
   end
return true
end
add this to the script and then change "thing.uid" to "target"
Lua:
local target = getTopCreature(position)

Edit:
test this script
Lua:
function onLook(cid, thing, position, lookDistance)
    local target = getTopCreature(position)
    if isPlayer(target) then
        local info = ""
        local health = (getCreatureHealth(target) / 1000)
        local maxhealth = (getCreatureMaxHealth(target) / 1000)
        local mana = (getPlayerMana(target) / 1000)
        local maxmana = (getPlayerMaxMana(target) / 1000)
        info = "".. getPlayerName(target) .." has ".. math.ceil(health) .."k/".. math.ceil(maxhealth) .."k health and ".. math.ceil(mana) .."k/".. math.ceil(maxmana) .."k mana!"
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, info)
        doCreatureSay(cid, info, TALKTYPE_MONSTER_SAY, false, cid, getThingPos(target))
    end
    return true
end
 
Thanks, but still having same error, I guess it returns the same result as thing.uid(?)

Lua:
function onLook(cid, thing, position, lookDistance)
    local stats = {
        info = "",
        target = (getTopCreature(position).uid),
        health = (getCreatureHealth(target) / 1000),
        maxhealth = (getCreatureMaxHealth(target) / 1000),
        mana = (getPlayerMana(target) / 1000),
        maxmana = (getPlayerMaxMana(target) / 1000)
    }

    if isPlayer(stats.target) then
        stats.info = "".. getPlayerName(stats.target) .." has ".. math.ceil(stats.health) .."k/".. math.ceil(stats.maxhealth) .."k health and ".. math.ceil(stats.mana) .."k/".. math.ceil(stats.maxmana) .."k mana!"
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, stats.info)
        doCreatureSay(cid, stats.target, TALKTYPE_MONSTER_SAY, false, cid, getThingPos(stats.target))
    end

return true
end

error:

Screenshot
 
Thanks, but still having same error, I guess it returns the same result as thing.uid(?)

Lua:
function onLook(cid, thing, position, lookDistance)
    local stats = {
        info = "",
        target = (getTopCreature(position).uid),
        health = (getCreatureHealth(target) / 1000),
        maxhealth = (getCreatureMaxHealth(target) / 1000),
        mana = (getPlayerMana(target) / 1000),
        maxmana = (getPlayerMaxMana(target) / 1000)
    }

    if isPlayer(stats.target) then
        stats.info = "".. getPlayerName(stats.target) .." has ".. math.ceil(stats.health) .."k/".. math.ceil(stats.maxhealth) .."k health and ".. math.ceil(stats.mana) .."k/".. math.ceil(stats.maxmana) .."k mana!"
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, stats.info)
        doCreatureSay(cid, stats.target, TALKTYPE_MONSTER_SAY, false, cid, getThingPos(stats.target))
    end

return true
end

error:

Screenshot
you have to change the getCreatureHealth(target) to getCreatureHealth(stats.target) and all the other aswell.
so the functions know where the variable/table is located

example:
Lua:
local stats = {
        info = "",
        target = (getTopCreature(position).uid),
        health = (getCreatureHealth(stats.target) / 1000),
        maxhealth = (getCreatureMaxHealth(stats.target) / 1000),
        mana = (getPlayerMana(stats.target) / 1000),
        maxmana = (getPlayerMaxMana(stats.target) / 1000)
    }
 
Screenshot

They're all inside same table so you must use target instead of stats.target xd
I did test this script and it did work.
Lua:
function onLook(cid, thing, position, lookDistance)
    local target = getTopCreature(position).uid
    if isPlayer(target) then
        local info = ""
        local health = (getCreatureHealth(target) / 1000)
        local maxhealth = (getCreatureMaxHealth(target) / 1000)
        local mana = (getPlayerMana(target) / 1000)
        local maxmana = (getPlayerMaxMana(target) / 1000)
        info = "".. getPlayerName(target) .." has ".. math.ceil(health) .."k/".. math.ceil(maxhealth) .."k health and ".. math.ceil(mana) .."k/".. math.ceil(maxmana) .."k mana!"
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, info)
        doCreatureSay(cid, info, TALKTYPE_MONSTER, false, cid, getCreaturePosition(target))
    end
    return true
end

Edit:
Updated the script to work whitout any errors
 
Last edited:
Solution
oh I see, I should have made the config inside the function and not outside xd

it also works like this:

Lua:
    local target = getTopCreature(position).uid
    if isPlayer(target) then
        local stats = {
            info = "",
            health = (getCreatureHealth(target) / 1000),
            maxhealth = (getCreatureMaxHealth(target) / 1000),
            mana = (getPlayerMana(target) / 1000),
            maxmana = (getPlayerMaxMana(target) / 1000)
        }
    stats.info = "".. getPlayerName(target) .." has ".. math.ceil(stats.health) .."k/".. math.ceil(stats.maxhealth) .."k health and ".. math.ceil(stats.mana) .."k/".. math.ceil(stats.maxmana) .."k mana!"
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, stats.info)
    doCreatureSay(cid, target, TALKTYPE_MONSTER_SAY, false, cid, getThingPos(target))
    end

thanks <3
 
oh I see, I should have made the config inside the function and not outside xd

it also works like this:

Lua:
    local target = getTopCreature(position).uid
    if isPlayer(target) then
        local stats = {
            info = "",
            health = (getCreatureHealth(target) / 1000),
            maxhealth = (getCreatureMaxHealth(target) / 1000),
            mana = (getPlayerMana(target) / 1000),
            maxmana = (getPlayerMaxMana(target) / 1000)
        }
    stats.info = "".. getPlayerName(target) .." has ".. math.ceil(stats.health) .."k/".. math.ceil(stats.maxhealth) .."k health and ".. math.ceil(stats.mana) .."k/".. math.ceil(stats.maxmana) .."k mana!"
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, stats.info)
    doCreatureSay(cid, target, TALKTYPE_MONSTER_SAY, false, cid, getThingPos(target))
    end

thanks <3
well since you only want the text to show if there is a player then you should check if its a player first and then make the calculations because if its not a player then there is no reason to calculate/assign values, and it will throw the error since the functions require a player uid/cid.

Im glad you got it to work :)
 
Back
Top