> For the complete documentation index, see [llms.txt](https://fehdead.gitbook.io/npc-kit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fehdead.gitbook.io/npc-kit/examples/talking-npc.md).

# A Merchant Who Remembers You

**NPCKit 0020 greets you and offers a gift on your first talk, then bows and greets you as a friend on every talk after it.**

This example takes place on map id 290, Rabanastre / Muthru Bazaar.

Download: [talking-npc.lua](https://github.com/FelipeJulio/NPC-Kit/tree/main/examples/talking-npc.lua)

```lua
local ok, npc = pcall(require, "scripts.NPCKit.Helper")
if not ok then
    print("Couldn't install, NPC Kit is missing.")
    return
end

local welcome = [[
Welcome! Is this your first visit to the bazaar?
Would you take a small gift to remember it by?
]]

local welcomeChoices = { "Yes, please", "No, thanks" }

local farewell = [[
May it serve you well on the road ahead of you.
Come back and tell me all about your travels.
]]

local refused = [[
Another time, then. The offer stays open, and
so does my stall, every single day until sundown.
]]

local again = [[
Good to see you again, friend! I hope the gift
served you well out there on the long, long road.
]]

local idle, talking, yes = 0x01000000, 0x01000002, 0x01000014

local potion = 0x0000

local function placeNpc(model, x, y, z, heading)
    npc.setautorelax(1);
    npc.usecharhit(0);
    npc.bindp2_d4(model, 4, 3);
    npc.stdmotionread(16);
    npc.stdmotionreadsync();
    npc.stdmotionplay_2c2(idle, 0);
    npc.setoverheadicontype(0);
    npc.usemapid(1);
    npc.usecharhit(1);
    npc.setweight(-1);
    npc.set_ignore_hitgroup(1);
    npc.motionloop(1);
    npc.setpos2(x, y, z);
    npc.dir(heading);
    npc.fetchambient();
    npc.bonecalcforce(-1);
    npc.clearhidecomplete();
    npc.show();
    npc.showshadow();
end

local function talkOpen()
    npc.sethpmenu(0);
    npc.ucoff();
    npc.settrapshowstatus(0);
    npc.setaturnlookatlockstatus(0);
    npc.raturna(-1);
    npc.turnsync();
    npc.stdmotionplay_2c2(talking, 20);
    npc.setkutipakustatus(1);
    npc.setunazukistatus(1);
end

local function talkClose()
    npc.setkutipakustatus(0);
    npc.setunazukistatus(0);
    npc.lookatoff();
    npc.ucon();
    npc.sethpmenu(1);
    npc.clear_force_char_nearfade();
    npc.setmaphighmodeldepth(-1);
    npc.setmapmodelstatus(1);
    npc.setstatuserrordispdenystatus(0);
    npc.settrapshowstatus(1);
    npc.stdmotionplay_2c2(idle, 20);
end

local function talkTerm()
    npc.setaturnlookatlockstatus(1);
    npc.aturn_261(npc.getdestrotangzx_227(0));
end

local function say(handle, body, onDone)
    if not dialog.amese.open {
            speaker = "NPCKit 0020",
            body = body,
            queue = true,
            onClose = onDone or function() handle.finish() end,
            onError = function() handle.finish() end
        } then
        handle.finish()
    end
end

local function ask(handle, body, onYes)
    if not dialog.aaske.open {
            speaker = "NPCKit 0020",
            body = body,
            choice = welcomeChoices,
            cancel = 1,
            queue = true,
            onClose = function(index, reason)
                if reason == "dismissed" and index == 0 then
                    onYes()
                else
                    say(handle, refused)
                end
            end,
            onError = function() handle.finish() end
        } then
        handle.finish()
    end
end

local function giveItem(item, count)
    npc.musictrans(-1, 30, 32);
    npc.setstatuserrorctrldenystatus_by_menu(1);
    npc.seteventtimestop(1);
    npc.moveblurcaptureonce();
    npc.movebluraplpha(1);
    npc.moveblurfade(15);
    npc.moveblurfadesync();
    npc.sebsoundplay(0, 40);
    npc.additemmes(item, count);
    npc.musictrans(-1, 30, 127);
    npc.moveblurclearfade(15);
    npc.moveblurfadesync();
    npc.seteventtimestop(0);
    npc.setstatuserrorctrldenystatus_by_menu(0);
end

local gaveGift = false

local function myMod()
    npc.add {
        id = "npckit_0020",
        map = 290,
        name = "NPCKit 0020",

        init = function()
            placeNpc(0x006E0019, 35.4, -9.0, 65.5, 1.676);
        end,

        main = function()
            npc.wait(90);
        end,

        bow = function()
            npc.stdmotionplay_2c2(yes, 20);
            npc.wait(40);
            npc.stdmotionplay_2c2(talking, 20);
        end,

        give = function()
            giveItem(potion, 1);
        end,

        talk = function()
            talkOpen();
            npc.lua(function(handle)
                if gaveGift then
                    handle.run("bow", function()
                        say(handle, again)
                    end)
                    return
                end
                ask(handle, welcome, function()
                    handle.run("give", function()
                        gaveGift = true
                        say(handle, farewell)
                    end)
                end)
            end);
            talkClose();
        end,

        talkterm = talkTerm
    }
end

event.registerEventAsync("onInitDone", myMod)
```

`ask` and `say` wrap `dialog.aaske` and `dialog.amese` so that every way out of a window finishes the talk. `gaveGift` is plain Lua state: it lives while the game runs, not in the save. The talk runs the actor functions `give` and `bow` with `handle.run`, and the gift is given the way vanilla gives one: music down, time stopped, the screen blurred, then `additemmes` and its notice.
