• 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 [tfs 1.3] combine 2 items NPC

Cebal

Member
Joined
Aug 28, 2009
Messages
18
Solutions
2
Reaction score
8
Hello guys! Maybe someone will be able to help with with a pretty simple script. Im looking for NPC which can combine 2 specific items to 1 other item. It would be nice to have an option to talk to this NPC and if I have 20 of each items I could exchange 20 at once, or less. Thanks!
 
Solution
Try this, and let me know how it goes. :)

XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Combiner" script="combiner.lua" walkinterval="2000" floorchange="0">
    <health now="100" max="100" />
    <look type="138" head="57" body="59" legs="40" feet="76" addons="0" />
</npc>
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()...
Try this, and let me know how it goes. :)

XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Combiner" script="combiner.lua" walkinterval="2000" floorchange="0">
    <health now="100" max="100" />
    <look type="138" head="57" body="59" legs="40" feet="76" addons="0" />
</npc>
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 function greetCallback(cid)
    npcHandler.topic[cid] = 0
    return true
end

local playerResponse = {}
local combinationItems = {2461, 2467} -- itemId's
local resultItem = 2173 -- itemId

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

    if msgcontains(msg, "combine") then
        npcHandler:say("How many " .. ItemType(combinationItems[1]):getPluralName() .. " and " .. ItemType(combinationItems[2]):getPluralName() .. " would you like to combine?", cid)
        npcHandler.topic[cid] = 1
       
    elseif npcHandler.topic[cid] == 1 then
        local amount = tonumber(msg)
        if amount == nil or amount < 1 then
            npcHandler:say("That is not an amount I am familiar with. Can you translate {" .. msg .. "} into my local currency?", cid)
            return true
        end
        npcHandler:say("Are you sure you would like to combine " .. msg .. " of each these items into " .. msg .. " " .. ItemType(resultItem):getPluralName() .. "? This process cannot be undone.", cid)
        npcHandler.topic[cid] = 2
        playerResponse[cid].amount = amount
       
    elseif npcHandler.topic[cid] == 2 then
        if not msgcontains(msg, "yes") then
            npcHandler:say("Another time, then.", cid)
            npcHandler.topic[cid] = 0
            playerResponse[cid] = {}
            return true
        end
        for i = 1, 2 do
            if player:getItemCount(combinationItems[i]) < playerResponse[cid].amount then
                npcHandler:say("You don't seem to have enough " .. ItemType(combinationItems[i]):getPluralName() .. " in your inventory for this transaction. How many were you wanting to combine today?", cid)
                npcHandler.topic[cid] = 1
                playerResponse[cid] = {}
                return true
            end
        end
        for i = 1, 2 do
            player:removeItem(combinationItems[i], playerResponse[cid].amount)
        end
        player:addItem(resultItem, playerResponse[cid].amount, true)
        npcHandler:say("Great! The items have all been combined into " .. playerResponse[cid].amount .. " " .. ItemType(resultItem):getPluralName() .. ". Let me know if you want to combine more items in the future!", cid)
        npcHandler.topic[cid] = 0
        playerResponse[cid] = {}
       
    end
    return true
end

local function onAddFocus(cid)
    playerResponse[cid] = {}
end

local function onReleaseFocus(cid)
    playerResponse[cid] = nil
end

npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)

npcHandler:setMessage(MESSAGE_GREET, "Welcome back |PLAYERNAME|. Are you looking to {combine} some items today?")
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
Back
Top