• 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 Can't sell item to NPC (Avesta 0.6.5)

auda

Advanced OT User
Joined
Jun 29, 2014
Messages
273
Reaction score
233
Location
Sweden
Every NPC is able to sell you items, but when you try to sell to the NPC they deny and replies as written in the scripts, "You do not have one.", even though I do.

What area I think is the problem and need to be edited:
Code:
        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('Thank you. Here is your gold.')
                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('Goodbye.', _delay)
            end

Entire script:
Code:
_state = 0
_count = 0
_index = 0
_delay = 1500

local items = {}
BLA BLA BLA ITEMS

function getNext()
    nextPlayer = getQueuedPlayer()
    if (nextPlayer ~= nil) then
        if (getDistanceToCreature(nextPlayer) <= 0) 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('Welcome to Diane\'s city jewelry. How may I help you?', delay)
end

function onCreatureAppear(cid)
end

function onCreatureDisappear(cid)
    if (getNpcFocus() == cid) then
        selfSay('Goodbye.', _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('You will have to wait, ' .. getCreatureName(cid) .. '.', _delay)
            queuePlayer(cid)
        end
      
    else
        if (msgcontains(msg, 'bye')) then
            selfSay('Goodbye.', _delay)
            getNext()
      
        elseif (msgcontains(msg, 'diane')) then
            _selfSay('I\'m here.')

        elseif (msgcontains(msg, 'offer')) then
            _selfSay('I sell and buy various types of amulets, necklaces and rings.')          
          
        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('There you go.', _delay)
                else
                    selfSay('Come back when you can afford my wares.', _delay)
                end
              
                updateNpcIdle()
            else
                selfSay('Hmm, 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('Thank you. Here is your gold.')
                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('Goodbye.', _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('Goodbye.', _delay)
            getNext()
        end
    end
end

Edit: There is NO error in the console.
 
Code:
if (_count > 1) then

Count what? I'm not sure because I don't look at the full scripts either do I know how avesta works, but should it not need an parameter of item ID to know what to count?
 
Code:
if (_count > 1) then

Count what? I'm not sure because I don't look at the full scripts either do I know how avesta works, but should it not need an parameter of item ID to know what to count?

If you mean that "parameters of item ID" is the same as items they sell/buy, then it's the "local items = {}" you mean? I removed the items just to shorten the length of the thread.
 
Back
Top