• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

[YurOTS 7.6] NPC - Change Items

FearWar

Active Member
Joined
Sep 17, 2017
Messages
221
Reaction score
26
Location
Brasil
Hi guys,

I need some help, I need an NPC to exchange certain items.

I will post a npcs base of ot ...

ITEMS:
  • magic longsword ID: 2390
  • hammer of wrath ID: 2444
  • great halberd ID: 2443

Trading system
Script should work this way
magic longsword > hammer of wrath > great halberd
hammer of wrath > magic longsword > great halberd
great halberd > magic longsword > hammer of wrath

an example how it should work:
PLAYER: Hi
NPC:('Hello ' PLAYER .. '! I exchange the quest items, say {change}!')
PLAYER: change
NPC:I can exchange the following items, magic longsword, hammer of wrath, great halberd!
PLAYER: magic longsword
NPC: Do you want to change "magic longsword" by what item?
PLAYER: hammer of wrath
NPC: His longsword magic was traded by hammer of wrath.



data/npc
XML:
<?xml version="1.0"?>
<npc name="A Sweaty Cyclops" script="data/npc/scripts/Konddor/a sweaty cyclops.lua" access="3">
    <look type="22" head="0" body="0" legs="0" feet="0" addons="0"/>
</npc>
data/npc/scripts/Konddor/a sweaty cyclops.lua
LUA:
focus = 0
talk_start = 0
target = 0
following = false
attacking = false

function onThingMove(creature, thing, oldpos, oldstackpos)
end
function onCreatureAppear(creature)
end
function onCreatureDisappear(cid, pos)
      if focus == cid then
          selfSay('Good bye then.')
          focus = 0
          talk_start = 0
      end
end
function onCreatureTurn(creature)
end
function msgcontains(txt, str)
      return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))
end
function onCreatureSay(cid, type, msg)
      msg = string.lower(msg)
      if (msgcontains(msg, 'hi') and (focus == 0)) and getDistanceToCreature(cid) < 4 then
          selfSay('Hello ' .. creatureGetName(cid) .. '! I exchange the quest items, say {change}!')
          focus = cid
          talk_start = os.clock()
      elseif msgcontains(msg, 'hi') and (focus ~= cid) and getDistanceToCreature(cid) < 4 then
          selfSay('Sorry, ' .. creatureGetName(cid) .. '! I talk to you in a minute.')
      elseif focus == cid then
        talk_start = os.clock()
            
      if string.find(msg, '(%a*)bye(%a*)') and focus == cid and getDistanceToCreature(cid) < 3 then
          selfSay('Good bye, ' .. creatureGetName(cid) .. '!')
          focus = 0
          talk_start = 0
      end
    end
end
function onCreatureChangeOutfit(creature)
end
function onThink()
      if (os.clock() - talk_start) > 30 then
          if focus > 0 then
              selfSay('Next Please...')
          end
          focus = 0
      end
     if focus ~= 0 then
         if getDistanceToCreature(focus) > 5 then
             selfSay('Good bye then.')
             focus = 0
         end
     end
end

39/5000



Thank you in advance for helping me!
 
Solution
Try this (yes I know it could be scripted a billion times better)

LUA:
local focus = 0
local talk_start = 0
local target = 0
local following = false
local attacking = false
local topic


function msgcontains(txt, str)
    return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))
end

function onThingMove(creature, thing, oldpos, oldstackpos) end
function onCreatureAppear(creature) end
function onCreatureTurn(creature) end
function onCreatureChangeOutfit(creature) end

function onCreatureDisappear(cid, pos)
    if focus == cid then
        selfSay('Good bye then.')
        focus = 0
        talk_start = 0
    end
end

function onThink()
    if (os.clock() - talk_start) > 30 then...
WOULD IT not be easier to make actions script that onUse change item to next item? making these 3 like 1 weapon that switches on click? you dont want player to pay npc anything to change so what is point of npc?

(obi npc sells parcels that change into house items so use trans_inc for example script :))
 
Try this (yes I know it could be scripted a billion times better)

LUA:
local focus = 0
local talk_start = 0
local target = 0
local following = false
local attacking = false
local topic


function msgcontains(txt, str)
    return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))
end

function onThingMove(creature, thing, oldpos, oldstackpos) end
function onCreatureAppear(creature) end
function onCreatureTurn(creature) end
function onCreatureChangeOutfit(creature) end

function onCreatureDisappear(cid, pos)
    if focus == cid then
        selfSay('Good bye then.')
        focus = 0
        talk_start = 0
    end
end

function onThink()
    if (os.clock() - talk_start) > 30 then
        if focus > 0 then
            selfSay('Next Please...')
        end
        focus = 0
    end
    if focus ~= 0 then
        if getDistanceToCreature(focus) > 5 then
            selfSay('Good bye then.')
            focus = 0
        end
    end
end

local isNear = function(cid)
    return getDistanceToCreature(cid) < 4
end


function onCreatureSay(cid, type, msg)
    msg = string.lower(msg)
    if not isNear(cid) then
        return true
    end

    if focus ~= cid and focus ~= 0 then
        selfSay('Sorry, ' .. creatureGetName(cid) .. '! I talk to you in a minute.')
        return true
    end

    local ITEM_MAGIC_LONGSWORD = 2390
    local ITEM_HAMMER_OF_WRATH = 2444
    local ITEM_GREAT_HALBERD = 2443

    local t

    if focus == 0 then
        if msgcontains(msg, 'hi') or msgcontains(msg, 'hello') then
            selfSay('Hello ' .. creatureGetName(cid) .. '! I exchange the quest items, say {change}!')
            focus = cid
            topic = 0
        end
    else
        if string.find(msg, '(%a*)bye(%a*)') then
            selfSay('Good bye, ' .. creatureGetName(cid) .. '!')
            focus = 0
            talk_start = 0
            return true
        elseif msgcontains(msg, "change") then
            selfSay("I can exchange the following items, magic longsword, hammer of wrath, great halberd!")
            t = 1
        elseif topic == 1 then
            if msgcontains(msg, "magic longsword") then
                selfSay("What weapon would you like to exchange your magic longsword for?")
                t = 2
            elseif msgcontains(msg, "hammer of wrath") then
                selfSay("What weapon would you like to exchange your hammer of wrath for?")
                t = 3
            elseif msgcontains(msg, "great halberd") then
                selfSay("What weapon would you like to exchange your great halberd for?")
                t = 4
            else
                selfSay("I only trade magic longswords, hammers of wrath or great halberds.")
            end
      
        -- Answer topic 2 magic longsword
        elseif topic == 2 then
            if msgcontains(msg, "hammer of wrath") then
                selfSay("Do you want to trade your magic longsword for a hammer of wrath?")
                t = 5
            elseif msgcontains(msg, "great halberd") then
                selfSay("Do you want to trade your magic longsword for a great halberd?")
                t = 6
            else
                selfSay("I only accept either a hammer of wrath or a great halberd.")
                t = 2
            end
        -- Answer topic 3 hammer of wrath
        elseif topic == 3 then
            if msgcontains(msg, "magic longsword") then
                selfSay("Do you want to trade your hammer of wrath for a magic longsword?")
                t = 7
            elseif msgcontains(msg, "great halberd") then
                selfSay("Do you want to trade your hammer of wrath for a great halberd?")
                t = 8                     
            else
                selfSay("I only accept either a magic longsword or a great halberd.")
                t = 3
            end
        -- Answer topic 4 great halberd
        elseif topic == 4 then
            if msgcontains(msg, "magic longsword") then
                selfSay("Do you want to trade your great halberd for a magic longsword?")
                t = 9
            elseif msgcontains(msg, "hammer of wrath") then
                selfSay("Do you want to trade your great halberd for a hammer of wrath?")
                t = 10
            else
                selfSay("I only accept either a magic longsword or a hammer of wrath.")
                t = 4
            end
      
        -- Confirm topic 5 (MLS)
        elseif topic == 5 then
            if msgcontains(msg, "yes") then
                if doPlayerRemoveItem(cid, ITEM_MAGIC_LONGSWORD) == 0 then
                    selfSay("Here you are, a hammer of wrath.")
                    buy(cid, ITEM_HAMMER_OF_WRATH, 1, 0)
                else
                    selfSay("You don't have a magic longsword.")
                end
            else
                selfSay("Hmm, maybe another time.")
            end
        -- Confirm topic 6 (MLS)
        elseif topic == 6 then
            if msgcontains(msg, "yes") then
                if doPlayerRemoveItem(cid, ITEM_MAGIC_LONGSWORD) == 0 then
                    selfSay("Here you are, a great halberd.")
                    buy(cid, ITEM_GREAT_HALBERD, 1, 0)
                else
                    selfSay("You don't have a magic longsword.")
                end
            else
                selfSay("Hmm, maybe another time.")
            end
        -- Confirm topic 7 (HOW)
        elseif topic == 7 then
            if msgcontains(msg, "yes") then
                if doPlayerRemoveItem(cid, ITEM_HAMMER_OF_WRATH) == 0 then
                    selfSay("Here you are, a magic longsword.")
                    buy(cid, ITEM_MAGIC_LONGSWORD, 1, 0)
                else
                    selfSay("You don't have a hammer of wrath.")
                end
            else
                selfSay("Hmm, maybe another time.")
            end
        -- Confirm topic 8 (HOW)
        elseif topic == 8 then
            if msgcontains(msg, "yes") then
                if doPlayerRemoveItem(cid, ITEM_HAMMER_OF_WRATH) == 0 then
                    selfSay("Here you are, a great halberd.")
                    buy(cid, ITEM_GREAT_HALBERD, 1, 0)
                else
                    selfSay("You don't have a hammer of wrath.")
                end
            else
                selfSay("Hmm, maybe another time.")
            end
        -- Confirm topic 9 (GH)
        elseif topic == 9 then
            if msgcontains(msg, "yes") then
                if doPlayerRemoveItem(cid, ITEM_GREAT_HALBERD) == 0 then
                    selfSay("Here you are, a magic longsword.")
                    buy(cid, ITEM_MAGIC_LONGSWORD, 1, 0)
                else
                    selfSay("You don't have a great halberd.")
                end
            else
                selfSay("Hmm, maybe another time.")
            end
        -- Confirm topic 10 (GH)
        elseif topic == 10 then
            if msgcontains(msg, "yes") then
                if doPlayerRemoveItem(cid, ITEM_GREAT_HALBERD) == 0 then
                    selfSay("Here you are, a hammer of wrath.")
                    buy(cid, ITEM_HAMMER_OF_WRATH, 1, 0)
                else
                    selfSay("You don't have a great halberd.")
                end
            else
                selfSay("Hmm, maybe another time.")
            end
        end
    end

    topic = t
    talk_start = os.clock()
    return true
end
 
Last edited:
Solution
Seems like doPlayerAddItem isn't registered in NPC class. Made a work-around using the "buy" function. Try again.
 
I found some yurots 0.9.4 ish version on github. What I can see the doPlayerRemoveItem function in NPC returns 0 if successful and -1 if unsuccessful. Pretty weird but I updated the script now.

Try again, and if it works then try again when you don't have any item - just to make sure.
 
I found some yurots 0.9.4 ish version on github. What I can see the doPlayerRemoveItem function in NPC returns 0 if successful and -1 if unsuccessful. Pretty weird but I updated the script now.

Try again, and if it works then try again when you don't have any item - just to make sure.

thanks bro, It worked!
 
I found some yurots 0.9.4 ish version on github. What I can see the doPlayerRemoveItem function in NPC returns 0 if successful and -1 if unsuccessful. Pretty weird but I updated the script now.

Try again, and if it works then try again when you don't have any item - just to make sure.


when it does not have the item, it gives this error in the console, is it normal?

36643
 
Back
Top