• 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 NPC Selling random item

DRUlD

New Member
Joined
Mar 4, 2018
Messages
16
Reaction score
0
Trying to add to the NPC to sell a random bag color
but sth. doesn't work out for me... Can somebody help me?

green bag 1991
yellow bag 1992
red bag 1993
etc.

Lua:
dofile(getDataDir() .. 'npc/scripts/lib/greeting.lua')

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

-- OTServ event handling functions
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 shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'scroll'}, 1949, 5)
shopModule:addBuyableItem({'bag'}, 1994, 5)


npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
Read the rules of the support forum and make sure to post your distro version next time to avoid the confusion.

I think this may work though, let me know.

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

-- OTServ event handling functions start
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 shopModule = ShopModule:new()...
Trying to add to the NPC to sell a random bag color
but sth. doesn't work out for me... Can somebody help me?

green bag 1991
yellow bag 1992
red bag 1993
etc.

Lua:
dofile(getDataDir() .. 'npc/scripts/lib/greeting.lua')

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

-- OTServ event handling functions
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 shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'scroll'}, 1949, 5)
shopModule:addBuyableItem({'bag'}, 1994, 5)


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

Lua:
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 bags = {1987, 1991, 1992, 1993, 1994, 1995, 1996}

local function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
    local cost
    for key, value in pairs(shop) do
        if item == value.id then
            cost = value.buy
            break
        end
    end

    local total_cost = amount * cost
    local item_name = ItemType(item):getName()
    local player = Player(cid)
    if player:removeMoney(total_cost) then
        player:addItem(item, amount, true, 1, CONST_SLOT_WHEREEVER)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Bought "..amount.."x "..item_name.." for "..total_cost.." gold.")
    end
    return true
end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local shop = {
        {name = "bag", id = bags[math.random(1, #bags)], subType = 0, buy = 10, sell = 0}
    }

    if msgcontains(msg, "trade") then
        openShopWindow(cid, shop, onBuy, onSell)
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Try this out.
 
in version 7.6 there was no trade window only by ( hi bag yes )
I'm just starting to learn lua, but maybe someone else will help me with rewriting this script for version 7.6

Every time I changed something errors in the console refer me to the npchandler.lua
npchandler.lua - Pastebin.com
 
Read the rules of the support forum and make sure to post your distro version next time to avoid the confusion.

I think this may work though, let me know.

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

-- OTServ event handling functions start
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 shopModule = ShopModule:new()
npcHandler:addModule(shopModule)
shopModule:addBuyableItem({'scroll'}, 1949, 5)

local talkState = {}
local bags = {1987, 1991, 1992, 1993, 1994, 1995, 1996}

local function creatureSayCallback(cid, type, msg)
    if(npcHandler.focus ~= cid) then
        return false
    end

    if not talkState[cid] then talkState[cid] = 0 end
    if talkState[cid] == 0 then
        if msgcontains(msg, "bag") then
            talkState[cid] = 1
            return true
        end
        return true
    end

    if talkState[cid] == 1 then
        if msgcontains(msg, "yes") then
            if doPlayerRemoveMoney(cid, 10) then
                doPlayerAddItem(cid, bags[math.random(1, #bags)], 1)
                npcHandler:say("Here you are.")
            else
                npcHandler:say("You don't have enough money.")
            end
        elseif msgcontains(msg, "no") then
            npcHandler:say("Ok then.")
        end
        talkState[cid] = 0
        return true
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
Back
Top