• 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 7.4 NPC what I have done wrong?

inp2007

Member
Joined
Jan 27, 2009
Messages
77
Reaction score
10
Location
Poland
I want to create a new NPC Frodo based on other script Xodet.lua.
Everythings works fine only when I would like to buy "ham,meat or bread" It's doesn't works.
But If I will say to Frodo "buy spellbook". Frodo's answer me and it work correct.
What I done wrong?He does not responding for ham,meat,bread.

XODET- original
Code:
<?xml version="1.0"?>
<npc name="Xodet" script="xodet.lua" walkinterval="2000" floorchange="0" idleinterval="30">
    <health now="100" max="100"/>
    <look type="130" head="19" body="86" legs="87" feet="95"/>
</npc>

Script:

Code:
_state = 0
_count = 0
_index = 0
_delay = 1000

items = {}
items[0] = {name = 'blank', id = 2260, subtype = -1, sell = 10, buy = -1}
items[1] = {name = 'rune of magic', id = 2281, subtype = -1, sell = 100, buy = -1}
items[2] = {name = 'spellbook', id = 2175, subtype = -1, sell = 150, buy = -1}


function getNext()
    nextPlayer = getQueuedPlayer()
    if (nextPlayer ~= nil) then
        if (getDistanceToCreature(nextPlayer) <= 4) then
            updateNpcIdle()
            setNpcFocus(nextPlayer)
            greet(nextPlayer, _delay * 3)
            return
        else
            getNext()
        end
    end
  
    setNpcFocus(0)
    resetNpcIdle()
end

local function onActionItem(action)
    if ((action == 'buy' and items[_index].sell == -1) or
        (action == 'sell' and items[_index].buy == -1)) then
        return
    end
  
    amount = ''
    suffix = ''
    plural = 'a'
  
    if (_count > 1) then
        amount = ' ' .. tostring(_count)
        suffix = 's'
        plural = ''
    end
  
    cost = items[_index].buy
    if (action == 'buy') then
        cost = items[_index].sell
    end
  
    selfSay('Do you want to ' .. action .. ' ' .. plural .. amount .. ' ' .. items[_index].name .. suffix .. ' for ' .. cost .. ' gold?')
end

function _selfSay(message)
    selfSay(message, _delay)
    updateNpcIdle()
end

local function greet(cid, delay)
    selfSay('Oh, please come in, ' .. getCreatureName(cid) .. '. What do you need?', delay)
end

function onCreatureAppear(cid)
end

function onCreatureDisappear(cid)
    if (getNpcFocus() == cid) then
        selfSay('Good bye and come again.', _delay)
        getNext()
    else
        unqueuePlayer(cid)
    end
end

function onCreatureMove(cid, oldPos, newPos)
    if (getNpcFocus() == cid) then
        faceCreature(cid)
    end
end

function onCreatureSay(cid, type, msg)
    if (getNpcFocus() == 0) then
        if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
            updateNpcIdle()
            setNpcFocus(cid)
            greet(cid, _delay)
        end
      
    elseif (getNpcFocus() ~= cid) then
        if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
            selfSay('Just wait, ' .. getCreatureName(cid) .. '.', _delay)
            queuePlayer(cid)
        end
      
    else
        if (msgcontains(msg, 'bye')) then
            selfSay('See you.', _delay)
            getNext()
      
        elseif (msgcontains(msg, 'name')) then
            _selfSay('I\'m Xodet the owner of this magic shop.')
      
        elseif (msgcontains(msg, 'job')) then
            _selfSay('I\'m sorcerer and trade with all kinds of magic items.')
          
        elseif (msgcontains(msg, 'manafluid')) then
            _selfSay('Sorry I do not sell manafluids anymore ask me about "Rune of Magic"!')  
          
        elseif (msgcontains(msg, 'time')) then
            _selfSay('It\'s ' .. getTibiaTime() .. ' right now.')
      
        elseif (msgcontains(msg, 'king') or msgcontains(msg, 'tibianus')) then
            _selfSay('The king has not much interest in magic items as far as I know.')
      
        elseif (msgcontains(msg, 'army')) then
            _selfSay('The army uses weapons and armor rather then items of magic.')
      
        elseif (msgcontains(msg, 'ferumbras')) then
            _selfSay('A hero has to be well prepared to face this threat.')
      
        elseif (msgcontains(msg, 'excalibug')) then
            _selfSay('Ah, I would trade a fortune for this fabulous item.')
      
        elseif (msgcontains(msg, 'thais')) then
            _selfSay('I am glad the king founded this academy far away from the mundane troubles of Thais')
      
        elseif (msgcontains(msg, 'tibia')) then
            _selfSay('The world is filled with wonderous places and items.')
      
        elseif (msgcontains(msg, 'carlin')) then
            _selfSay('I heard it\'s a city of druids.')
      
        elseif (msgcontains(msg, 'edron')) then
            _selfSay('In our town, science and arts are thriving.')
      
        elseif (msgcontains(msg, 'news') or msgcontains(msg, 'rumors')) then
            _selfSay('Ask for news and rumors in the tavern.')
      
        elseif (msgcontains(msg, 'offer') or msgcontains(msg, 'goods')) then
            _selfSay('I\'m selling blank runes, runes of magic and spellbooks.')
          
          
        elseif (_state == 1) then
            if (msgcontains(msg, 'yes')) then
                if (doPlayerRemoveMoney(cid, items[_index].sell * _count) == 1) then
                    for i = 1, _count do
                        doPlayerAddItem(cid, items[_index].id, items[_index].subtype)
                    end
              
                    selfSay('Here you are.', _delay)
                else
                    selfSay('Come back, when you have enough money.', _delay)
                end
              
                updateNpcIdle()
            else
                selfSay('Hmm, but next time.', _delay)
            end
          
            _state = 0
          
        elseif (_state == 2) then
            if (msgcontains(msg, 'yes')) then
                if (doPlayerRemoveItem(cid, items[_index].id, _count, items[_index].subtype) == 1) then
                    doPlayerAddMoney(cid, items[_index].buy * _count)
                    selfSay('Ok. Here is your money.')
                else
                    if (count > 1) then
                        selfSay('Sorry, you do not have so many.', _delay)
                    else
                        selfSay('Sorry, you do not have one.', _delay)
                    end
                end
              
                updateNpcIdle()
            else
                selfSay('Maybe next time.', _delay)
            end
      
            _state = 0
          
        else
            for n = 0, table.getn(items) do
                if (msgcontains(msg, items[n].name) or msgcontains(msg, items[n].name .. "s")) then
                    _count = getCount(msg)
                    _index = n
                  
                    if (msgcontains(msg, 'sell')) then
                        onActionItem('sell')
                        _state = 2
                    else
                        onActionItem('buy')
                        _state = 1
                    end
                  
                    updateNpcIdle()
                    break
                end
            end
        end
    end
end

function onThink()
    if (getNpcFocus() ~= 0) then
        if (isNpcIdle() or getDistanceToCreature(getNpcFocus()) > 4) then
            selfSay('Good bye and come again.', _delay)
            getNext()
        end
    end
end

And now my NPC-based on Xodet- Frodo:

Code:
<?xml version="1.0"?>
<npc name="Frodo" script="frodo.lua" walkinterval="2000" floorchange="0" idleinterval="30">
    <health now="100" max="100"/>
    <look type="128" head="58" body="68" legs="71" feet="132"/>
</npc>

And the script in which i JUST changed id for food and prices.
rest is the same
Code:
_state = 0
_count = 0
_index = 0
_delay = 1000

items = {}
items[0] = {name = 'meat', id = 2666, subtype = -1, sell = 10, buy = -1}
items[1] = {name = 'cheese', id = 2696, subtype = -1, sell = 50, buy = -1}
items[2] = {name = 'bread', id = 2689, subtype = -1, sell = 5, buy = -1}

Please HELP!
 
Code:
_state = 0
_count = 0
_index = 0
_delay = 1000

items = {}
items[0] = {name = 'meat', id = 2666, subtype = -1, sell = 10, buy = -1}
items[1] = {name = 'cheese', id = 2696, subtype = -1, sell = 50, buy = -1}
items[2] = {name = 'bread', id = 2689, subtype = -1, sell = 5, buy = -1}


function getNext()
    nextPlayer = getQueuedPlayer()
    if (nextPlayer ~= nil) then
        if (getDistanceToCreature(nextPlayer) <= 4) then
            updateNpcIdle()
            setNpcFocus(nextPlayer)
            greet(nextPlayer, _delay * 3)
            return
        else
            getNext()
        end
    end
   
    setNpcFocus(0)
    resetNpcIdle()
end

local function onActionItem(action)
    if ((action == 'buy' and items[_index].sell == -1) or
        (action == 'sell' and items[_index].buy == -1)) then
        return
    end
   
    amount = ''
    suffix = ''
    plural = 'a'
   
    if (_count > 1) then
        amount = ' ' .. tostring(_count)
        suffix = 's'
        plural = ''
    end
   
    cost = items[_index].buy
    if (action == 'buy') then
        cost = items[_index].sell
    end
   
    selfSay('Do you want to ' .. action .. ' ' .. plural .. amount .. ' ' .. items[_index].name .. suffix .. ' for ' .. cost .. ' gold?')
end

function _selfSay(message)
    selfSay(message, _delay)
    updateNpcIdle()
end

local function greet(cid, delay)
    selfSay('Oh, please come in, ' .. getCreatureName(cid) .. '. What do you need?', delay)
end

function onCreatureAppear(cid)
end

function onCreatureDisappear(cid)
    if (getNpcFocus() == cid) then
        selfSay('Good bye and come again.', _delay)
        getNext()
    else
        unqueuePlayer(cid)
    end
end

function onCreatureMove(cid, oldPos, newPos)
    if (getNpcFocus() == cid) then
        faceCreature(cid)
    end
end

function onCreatureSay(cid, type, msg)
    if (getNpcFocus() == 0) then
        if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
            updateNpcIdle()
            setNpcFocus(cid)
            greet(cid, _delay)
        end
       
    elseif (getNpcFocus() ~= cid) then
        if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
            selfSay('Just wait, ' .. getCreatureName(cid) .. '.', _delay)
            queuePlayer(cid)
        end
       
    else
        if (msgcontains(msg, 'bye')) then
            selfSay('See you.', _delay)
            getNext()
       
        elseif (msgcontains(msg, 'name')) then
            _selfSay('I\'m Xodet the owner of this magic shop.')
       
        elseif (msgcontains(msg, 'job')) then
            _selfSay('I\'m sorcerer and trade with all kinds of magic items.')
           
        elseif (msgcontains(msg, 'manafluid')) then
            _selfSay('Sorry I do not sell manafluids anymore ask me about "Rune of Magic"!')   
           
        elseif (msgcontains(msg, 'time')) then
            _selfSay('It\'s ' .. getTibiaTime() .. ' right now.')
       
        elseif (msgcontains(msg, 'king') or msgcontains(msg, 'tibianus')) then
            _selfSay('The king has not much interest in magic items as far as I know.')
       
        elseif (msgcontains(msg, 'army')) then
            _selfSay('The army uses weapons and armor rather then items of magic.')
       
        elseif (msgcontains(msg, 'ferumbras')) then
            _selfSay('A hero has to be well prepared to face this threat.')
       
        elseif (msgcontains(msg, 'excalibug')) then
            _selfSay('Ah, I would trade a fortune for this fabulous item.')
       
        elseif (msgcontains(msg, 'thais')) then
            _selfSay('I am glad the king founded this academy far away from the mundane troubles of Thais')
       
        elseif (msgcontains(msg, 'tibia')) then
            _selfSay('The world is filled with wonderous places and items.')
       
        elseif (msgcontains(msg, 'carlin')) then
            _selfSay('I heard it\'s a city of druids.')
       
        elseif (msgcontains(msg, 'edron')) then
            _selfSay('In our town, science and arts are thriving.')
       
        elseif (msgcontains(msg, 'news') or msgcontains(msg, 'rumors')) then
            _selfSay('Ask for news and rumors in the tavern.')
       
        elseif (msgcontains(msg, 'offer') or msgcontains(msg, 'goods')) then
            _selfSay('I\'m selling blank runes, runes of magic and spellbooks.')
           
           
        elseif (_state == 1) then
            if (msgcontains(msg, 'yes')) then
                if (doPlayerRemoveMoney(cid, items[_index].sell * _count) == 1) then
                    for i = 1, _count do
                        doPlayerAddItem(cid, items[_index].id, items[_index].subtype)
                    end
               
                    selfSay('Here you are.', _delay)
                else
                    selfSay('Come back, when you have enough money.', _delay)
                end
               
                updateNpcIdle()
            else
                selfSay('Hmm, but next time.', _delay)
            end
           
            _state = 0
           
        elseif (_state == 2) then
            if (msgcontains(msg, 'yes')) then
                if (doPlayerRemoveItem(cid, items[_index].id, _count, items[_index].subtype) == 1) then
                    doPlayerAddMoney(cid, items[_index].buy * _count)
                    selfSay('Ok. Here is your money.')
                else
                    if (count > 1) then
                        selfSay('Sorry, you do not have so many.', _delay)
                    else
                        selfSay('Sorry, you do not have one.', _delay)
                    end
                end
               
                updateNpcIdle()
            else
                selfSay('Maybe next time.', _delay)
            end
       
            _state = 0
           
        else
            for n = 0, table.getn(items) do
                if (msgcontains(msg, items[n].name) or msgcontains(msg, items[n].name .. "s")) then
                    _count = getCount(msg)
                    _index = n
                   
                    if (msgcontains(msg, 'sell')) then
                        onActionItem('sell')
                        _state = 2
                    else
                        onActionItem('buy')
                        _state = 1
                    end
                   
                    updateNpcIdle()
                    break
                end
            end
        end
    end
end

function onThink()
    if (getNpcFocus() ~= 0) then
        if (isNpcIdle() or getDistanceToCreature(getNpcFocus()) > 4) then
            selfSay('Good bye and come again.', _delay)
            getNext()
        end
    end
end
 
I think you didnt reload npcs after changes
Yes i do.

I also checked it by other try of make NPC. Bug is still the same i changed the item iD and names but NPC still selling the Xodet's equip while ask.


16:12 RSX: hi
16:12 Sam: Oh, please come in, RSX. What do you need?
16:12 RSX: sell mace
16:12 RSX: buy spellbook
16:12 Sam: Do you want to buy a spellbook for 150 gold?
16:12 RSX: yes
16:12 Sam: Here you are.
16:12 You see a spellbook.It weighs 58.00 oz.
 
Last edited:
Back
Top