• 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 Problem with NPCs

Vitich

Member
Joined
Nov 28, 2012
Messages
266
Reaction score
11
Hi, I've got a problem, If I got money in bank, npc sell me items but it doesn't remove from bank balance.

How can I fix that?


Thank you!

Account balance: 200000 gold.
NPC: 19:11 Bought 1x hallowed axe for 1000 gold.
Account balance: 200000 gold

NPC doesn't remove money from bank but It give me the item.
 
Last edited by a moderator:
Account balance: 200000 gold.
NPC: 19:11 Bought 1x hallowed axe for 1000 gold.
Account balance: 200000 gold

NPC doesn't remove money from bank but It give me the item.
Do you have money in your bp when you try this? Post the NPC lua/xml file that you're using also post npc lib.
 
No, if i got money with me, npc remove money from my backpack... But when I try bought something with bank balance, npc doesn't remove that money, but can buy items for free....

This is the npc.xml (it happens with all npc, not this one)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Xodet" script="Xodet.lua" walkinterval="2000" floorchange="0">
    <health now="100" max="100" />
    <look type="130" head="19" body="86" legs="87" feet="95" addons="0" />
    <parameters>
        <parameter key="module_shop" value="1" />
        <parameter key="shop_buyable" value="
       
            ultimate mp,26029,350;
            Supreme Health Potion,26031,500;
            ultimate spirit potion,26030,350;       
            animate dead rune,2316,375;
            disintegrate rune,2310,26;
            holy missile rune,2295,16;
            icicle rune,2271,30;
            paralyze rune,2278,700;
            wild growth rune,2269,160;           
           avalanche rune,2308,46;
            avalanche rune,2293,116;
            avalanche rune,2274,45;
            blank rune,2260,10;
            chameleon rune,2291,210;
            convince creature rune,2290,80;
            cure poison rune,2266,65;
            destroy field rune,2261,15;
            energy field rune,2277,38;
            energy wall rune,2279,85;
            explosion rune,2313,31;
            fire bomb rune,2305,117;
            fire field rune,2301,28;
            fire wall rune,2303,61;
            great fireball rune,2304,45;
            great health potion,7591,190;
            great mana potion,7590,120;
            great spirit potion,8472,190;
            health potion,7618,45;
            heavy magic missile rune,2311,12;
            intense healing rune,2265,95;
            light magic missile rune,2287,4;
            mana potion,7620,50;
            moonlight rod,2186,1000;
            necrotic rod,2185,5000;
            poison field rune,2285,21;
            poison wall rune,2289,52;
            snakebite rod,2182,500;
            spellbook,2175,150;
            stalagmite rune,2292,12;
            strong health potion,7588,100;
            strong mana potion,7589,80;
            sudden death rune,2268,50;
            terra rod,2181,10000;
            Hailstorm Rod,2183,15000;
            Wand of Voodoo,8922,22000;
                        underworld rod,8910,30000;
            ultimate healing rune,2273,175;
            ultimate health potion,8473,310;
            wand of cosmic energy,2189,10000;
            wand of decay,2188,5000;
            wand of dragonbreath,2191,1000;
            wand of vortex,2190,500;" />
        <parameter key="shop_sellable" value="
            empty potion flask,7634,5;
            empty potion flask,7635,5;
            empty potion flask,7636,5;
            spellwand,7735,299;
            vial,2006,5;" />
    </parameters>
</npc>

npc.lua

Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                npcHandler:onThink()                    end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    local items = {[1] = 2190, [2] = 2182}
    local itemId = items[player:getVocation():getBase():getId()]
    if msgcontains(msg, 'first rod') or msgcontains(msg, 'first wand') then
        if player:isMage() then
            if player:getStorageValue(Storage.firstMageWeapon) == -1 then
                npcHandler:say('So you ask me for a {' .. ItemType(itemId):getName() .. '} to begin your adventure?', cid)
                npcHandler.topic[cid] = 1
            else
                npcHandler:say('What? I have already gave you one {' .. ItemType(itemId):getName() .. '}!', cid)
            end
        else
            npcHandler:say('Sorry, you aren\'t a druid either a sorcerer.', cid)
        end
    elseif msgcontains(msg, 'yes') then
        if npcHandler.topic[cid] == 1 then
            player:addItem(itemId, 1)
            npcHandler:say('Here you are young adept, take care yourself.', cid)
            player:setStorageValue(Storage.firstMageWeapon, 1)
        end
        npcHandler.topic[cid] = 0
    elseif msgcontains(msg, 'no') and npcHandler.topic[cid] == 1 then
        npcHandler:say('Ok then.', cid)
        npcHandler.topic[cid] = 0
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

lib/npc.lua

Code:
-- Including the Advanced NPC System
dofile('data/npc/lib/npcsystem/npcsystem.lua')
dofile('data/npc/lib/npcsystem/customModules.lua')

isPlayerPremiumCallback = Player.isPremium

function msgcontains(message, keyword)
    local message, keyword = message:lower(), keyword:lower()
    if message == keyword then
        return true
    end

    return message:find(keyword) and not message:find('(%w+)' .. keyword)
end

function doNpcSellItem(cid, itemId, amount, subType, ignoreCap, inBackpacks, backpack)
    local amount = amount or 1
    local subType = subType or 0
    local item = 0
    local player = Player(cid)
    if ItemType(itemId):isStackable() then
        local stuff
        if inBackpacks then
            stuff = Game.createItem(backpack, 1)
            item = stuff:addItem(itemId, math.min(100, amount))
        else
            stuff = Game.createItem(itemId, math.min(100, amount))
        end

        return player:addItemEx(stuff, ignoreCap) ~= RETURNVALUE_NOERROR and 0 or amount, 0
    end

    local a = 0
    if inBackpacks then
        local container, itemType, b = Game.createItem(backpack, 1), ItemType(backpack), 1
        for i = 1, amount do
            local item = container:addItem(itemId, subType)
            if isInArray({(itemType:getCapacity() * b), amount}, i) then
                if player:addItemEx(container, ignoreCap) ~= RETURNVALUE_NOERROR then
                    b = b - 1
                    break
                end

                a = i
                if amount > i then
                    container = Game.createItem(backpack, 1)
                    b = b + 1
                end
            end
        end

        return a, b
    end

    for i = 1, amount do -- normal method for non-stackable items
        local item = Game.createItem(itemId, subType)
        if player:addItemEx(item, ignoreCap) ~= RETURNVALUE_NOERROR then
            break
        end
        a = i
    end
    return a, 0
end

local func = function(cid, text, type, e, pcid)
    local npc = Npc(cid)
    if not npc then
        return
    end

    local player = Player(pcid)
    if player then
        npc:say(text, type, false, player, npc:getPosition())
        e.done = true
    end
end

function doCreatureSayWithDelay(cid, text, type, delay, e, pcid)
    if Player(pcid) then
        e.done = false
        e.event = addEvent(func, delay < 1 and 1000 or delay, cid, text, type, e, pcid)
    end
end

function doPlayerTakeItem(cid, itemid, count)
    local player = Player(cid)
    if player:getItemCount(itemid) < count then
        return false
    end

    while count > 0 do
        local tempcount = 0
        if ItemType(itemid):isStackable() then
            tempcount = math.min (100, count)
        else
            tempcount = 1
        end

        local ret = player:removeItem(itemid, tempcount)
        if ret then
            count = count - tempcount
        else
            return false
        end
    end

    if count ~= 0 then
        return false
    end
    return true
end

function doPlayerSellItem(cid, itemid, count, cost)
    local player = Player(cid)
    if doPlayerTakeItem(cid, itemid, count) then
        if not player:addMoney(cost) then
            error('Could not add money to ' .. player:getName() .. '(' .. cost .. 'gp)')
        end
        return true
    end
    return false
end

function doPlayerBuyItemContainer(cid, containerid, itemid, count, cost, charges)
    local player = Player(cid)
    if not player:removeMoney(cost) then
        return false
    end

    for i = 1, count do
        local container = Game.createItem(containerid, 1)
        for x = 1, ItemType(containerid):getCapacity() do
            container:addItem(itemid, charges)
        end

        if player:addItemEx(container, true) ~= RETURNVALUE_NOERROR then
            return false
        end
    end
    return true
end

function getCount(string)
    local b, e = string:find("%d+")
    return b and e and tonumber(string:sub(b, e)) or -1
end
 
Neither script you provided contains code which will remove money from your bank nor does it even check for a balance of your bank account.
Why would you think that the npc would remove money from your bank account?
Are there errors in console? Could you post them if there are?
 
No, there isn't any error in console. Only NPC doesn't remove money from bank.

So I want that none npc check the money from bank (only the money inside backpack)

bump

Problem solved myself. Close thread.
 
Last edited by a moderator:
Back
Top