> 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/walking.md).

# Walking Back and Forth

**NPCKit 0008 walks four units east, waits, walks back, waits, forever. Talk to it and it shows you a dance move: three full spins, left, right and left.**

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

Download: [walking.lua](https://github.com/FelipeJulio/NPC-Kit/tree/main/examples/walking.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 offer = [[
Hey! Wanna see a dance move? I've been working
on it all week, and I think it's finally ready.
]]

local proud = [[
Not bad, huh? Three spins, left, right and left.
I'll be the talk of the whole bazaar by dusk.
]]

local idle, talking = 0x01000000, 0x01000002

local home = { 29.36, -9.0, 68.65 }

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 0008",
            body = body,
            queue = true,
            onClose = onDone or function() handle.finish() end,
            onError = function() handle.finish() end
        } then
        handle.finish()
    end
end

local function spin(way)
    for _, angle in ipairs({ 2.0944, -2.0944, 0.0 }) do
        npc.aturn_261(angle * way);
        npc.turnsync();
    end
end

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

        init = function()
            placeNpc(0x006E0012, home[1], home[2], home[3], 1.676);
        end,

        main = function()
            npc.aturn(home[1] + 4.0, home[2], home[3]);
            npc.move(home[1] + 4.0, home[2], home[3]);
            npc.wait(90);
            npc.aturn(home[1], home[2], home[3]);
            npc.move(home[1], home[2], home[3]);
            npc.wait(90);
        end,

        dance = function()
            npc.aturn_261(0.0);
            npc.turnsync();
            spin(1);
            spin(-1);
            spin(1);
            npc.raturna(-1);
            npc.turnsync();
        end,

        talk = function()
            talkOpen();
            npc.lua(function(handle)
                say(handle, offer, function()
                    handle.run("dance", function()
                        say(handle, proud)
                    end)
                end)
            end);
            talkClose();
        end,

        talkterm = talkTerm
    }
end

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

`main` repeats, and its `wait`s give the frames back. The talk runs the actor function `dance`. `aturn_261(angle)` turns to an angle with a motion and `turnsync` waits for the turn, so one full spin is three steps of 120 degrees (`spin`, a plain Lua function whose calls land in `dance`). An angle `a` faces `(sin a, cos a)` in `(x, z)`. `move` turns the NPC toward where it walks, so it can't walk backwards. `move` has no time limit, so keep the path clear: an NPC that is blocked stays where it is.
