• 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!

Event npc

Og1999

New Member
Joined
Apr 1, 2015
Messages
34
Reaction score
1
Hello!
I want to do a npc that sells items. But you can only buy these items with event tokens that you get from winning events like zombie, last man standing etc.
So can someone help me with this?

Iam very thankful if someone did :)
 
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
function onCreatureAppear(cid) npcHandler:eek:nCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:eek:nCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:eek:nCreatureSay(cid, type, msg) end
function onThink() npcHandler:eek:nThink() end
function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVbehavior == CONVERSATION_DEFAULT and 0 or cid
local shopWindow = {}
local moeda = YourTokensID
local t = {
[ItemID] = {price = 10},
}
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if t[item] and not doPlayerRemoveItem(cid, moeda, t[item].price) then
selfSay("you do not have "..t[item].price.." "..getItemNameById(moeda), cid)
else
doPlayerAddItem(cid, item)
selfSay("Here you go!", cid)
end
return true
end
if (msgcontains(msg, 'trade') or msgcontains(msg, 'TRADE'))then
for var, ret in pairs(t) do
table.insert(shopWindow, {id = var, subType = 0, buy = ret.price, sell = 0, name = getItemNameById(var)})
end
openShopWindow(cid, shopWindow, onBuy, onSell)
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

This works on TFS 0.3.6 I don't know for the rest.
 
EventNpc.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Event item seller" script="itemseller.lua" nugget="1" walkinterval="2000" floorchange="0">
    <health now="100" max="100"/>
    <look type="134" head="87" body="86" legs="86" feet="87" addons="3"/>
</npc>

Itemseller.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 talk = {}

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'Donator Helmet'}, 9778, 1700, 1, 'Donator Helmet')
shopModule:addBuyableItem({'Donator Armor'}, 9776, 2000, 1, 'Donator Armor')
shopModule:addBuyableItem({'Donator Legs'}, 9777, 2000, 1, 'Donator Legs')
shopModule:addBuyableItem({'Donator Boots'}, 11118, 1800, 1, 'Donator Boots')
shopModule:addBuyableItem({'Witchhunters Cloak'}, 8821, 2000, 1, 'Witchunters Cloak')
shopModule:addBuyableItem({'Donator Sword'}, 11307, 3000, 1, 'Donator Sword')
shopModule:addBuyableItem({'Donator Axe'}, 11323, 3000, 1, 'Donator Axe')
shopModule:addBuyableItem({'Donator Club'}, 11308, 3000, 1, 'Donator Club')
shopModule:addBuyableItem({'Donator Wand'}, 2453, 3000, 1, 'Donator Wand')
shopModule:addBuyableItem({'Donator Rod'}, 2184, 3000, 1, 'Donator Rod')
shopModule:addBuyableItem({'Donator Ring'}, 7697, 3650, 1, 'Donator Ring')
shopModule:addBuyableItem({'Donator Shield'}, 6433, 2000, 1, 'Donator Shield')
shopModule:addBuyableItem({'Donator Spellbook'}, 8903, 2000, 1, 'Donator Spellbook')
shopModule:addBuyableItem({'Donator Bow'}, 8858, 1000, 1, 'Donator Bow')
shopModule:addBuyableItem({'Donator Arrow'}, 2352, 2000, 1, 'Donator Arrow')
shopModule:addBuyableItem({'Frost Charm'}, 7289, 2800, 1, 'Frost Charm')
shopModule:addBuyableItem({'Donator Uh'}, 2270, 3000, 1, 'Donator Uh')
shopModule:addBuyableItem({'Donator Manarune'}, 2294, 3000, 1, 'Donator Manarune')









function callbackBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks)
    local shopItem = nil
    for _, item in ipairs(npcHandler.shopItems) do
        if(item.id == itemid and item.subType == subType) then
            shopItem = item
            break
        end
    end

    if(shopItem == nil) then
        print("[ShopModule.onBuy]", "Item not found on shopItems list")
        return false
    end

    if(shopItem.buy == -1) then
        print("[ShopModule.onSell]", "Attempt to purchase an item which only sellable")
        return false
    end

    if(amount <= 0) then
        print("[ShopModule.onSell]", "Attempt to purchase " .. amount .. " items")
        return false
    end

    local totalCost = amount * shopItem.buy
    local parseInfo = {
        [TAG_PLAYERNAME] = getPlayerName(cid),
        [TAG_ITEMCOUNT] = amount,
        [TAG_TOTALCOST] = totalCost,
        [TAG_ITEMNAME] = shopItem.name
    }

    if(getPlayerItemCount(cid, 2157) < totalCost) then
        local msg = npcHandler:getMessage(MESSAGE_NEEDMONEY)
        doPlayerSendCancel(cid, npcHandler:parseMessage(msg, parseInfo))
        return false
    end

    local subType = shopItem.subType or 1
    local a, b = doNpcSellItem(cid, itemid, amount, subType, ignoreCap, false, backpack)
    if(a < amount) then
        local msgId = MESSAGE_NEEDMORESPACE
        if(a == 0) then
            msgId = MESSAGE_NEEDSPACE
        end

        local msg = npcHandler:getMessage(msgId)
        parseInfo[TAG_ITEMCOUNT] = a

        doPlayerSendCancel(cid, npcHandler:parseMessage(msg, parseInfo))
        if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
            npcHandler.talkStart[cid] = os.time()
        else
            npcHandler.talkStart = os.time()
        end

        if(a > 0) then
            doPlayerRemoveItem(cid, 2157, a * shopItem.buy)
            return true
        end

        return false
    end

    local msg = npcHandler:getMessage(MESSAGE_BOUGHT)
    msg = string.reverse(string.gsub(string.reverse(msg), 'dlog', 'steggun dlog'))
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, npcHandler:parseMessage(msg, parseInfo))

    doPlayerRemoveItem(cid, 2157, totalCost)
    if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
        npcHandler.talkStart[cid] = os.time()
    else
        npcHandler.talkStart = os.time()
    end

    return true
end

function callbackSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)
    local shopItem = nil
    for _, item in ipairs(npcHandler.shopItems) do
        if(item.id == itemid and item.subType == subType) then
            shopItem = item
            break
        end
    end

    if(shopItem == nil) then
        print("[ShopModule.onBuy]", "Item not found on shopItems list")
        return false
    end

    if(shopItem.sell == -1) then
        print("[ShopModule.onSell]", "Attempt to sell an item which is only buyable")
        return false
    end

    local parseInfo = {
        [TAG_PLAYERNAME] = getPlayerName(cid),
        [TAG_ITEMCOUNT] = amount,
        [TAG_TOTALCOST] = amount * shopItem.sell,
        [TAG_ITEMNAME] = shopItem.name
    }

    if(subType < 1 or getItemInfo(itemid).stackable) then
        subType = -1
    end

    if(doPlayerRemoveItem(cid, itemid, amount, subType)) then
        local msg = npcHandler:getMessage(MESSAGE_SOLD)
        msg = string.reverse(string.gsub(string.reverse(msg), 'dlog', 'steggun dlog'))
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, npcHandler:parseMessage(msg, parseInfo))

        doPlayerAddItem(cid, 2157, amount * shopItem.sell)
        if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
            npcHandler.talkStart[cid] = os.time()
        else
            npcHandler.talkStart = os.time()
        end

        return true
    end

    local msg = npcHandler:getMessage(MESSAGE_NEEDITEM)
    doPlayerSendCancel(cid, npcHandler:parseMessage(msg, parseInfo))
    if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
        npcHandler.talkStart[cid] = os.time()
    else
        npcHandler.talkStart = os.time()
    end

    return false
end

npcHandler:setCallback(CALLBACK_ONBUY, callbackBuy)
npcHandler:setCallback(CALLBACK_ONSELL, callbackSell)
npcHandler:addModule(FocusModule:new())
 
Back
Top