• 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 LUA Config

Ezzz

Developer of Nostalrius and The Violet Project
Joined
Feb 26, 2010
Messages
1,954
Solutions
3
Reaction score
869
Location
Europe
Hey,

I want to use advanced lua scripts in my NPCs, but I don't know anything about LUA,
any idea how can I use this to make the NPC answer to Keys and reply with reply?

Code:
local Messages = {
     {Key = "name", Reply = "I'm Joe."},
     {Key = "job", Reply = "I'm a merchant."}
}
 
You can do this in the XML, it will give the same effect.

LUA:
<parameter value="1" key="module_keywords"/> 
<parameter value="name;job;help;something;" key="keywords"/>
<parameter key="keyword_reply1" value="I am Joe." />
<parameter key="keyword_reply2" value="I am a merchant." />
<parameter key="keyword_reply3" value="I can help you. " />
<parameter key="keyword_reply4" value="Right." />
 
Last edited:
You can do this in the XML, it will give the same effect.

LUA:
<parameter value="1" key="module_keywords"/> 
<parameter value="name;job;help;something;" key="keywords"/>
<parameter key="keyword_reply1" value="I am Joe." />
<parameter key="keyword_reply2" value="I am a merchant." />
<parameter key="keyword_reply3" value="I can help you. " />
<parameter key="keyword_reply4" value="Right." />

I know, but parameters in XML is pretty deprecated, it's missing talking delays too.
 
There are two ways of doing that, here's the full script of an example NPC using both modes:

-- First Mode --

Code:
-- MODE 1 --

local focuses, controlId = {}, 3586

local talkState = {}

function onCreatureDisappear(cid)
    if isFocused(cid, focuses) then
     removeFocus(cid, focuses)
        if isPlayer(cid) then
         closeShopWindow(cid)
        end
    end
end

local Messages = {
     {Key = "name", Reply = "I'm Joe."},
     {Key = "job", Reply = "I'm a merchant."}
}


function onCreatureSay(cid, type, msg)
 local travelWindow = {}
    if msgcontains(msg, "hi", "hello") and not isFocused(cid, focuses) and getDistanceToCreature(cid) < 4 then
       addFocus(cid, focuses)
       selfSay("Hiho " .. getCreatureName(cid) .. ", i'm the test npc.", cid)
     
    elseif msgcontains(msg, "bye") then
          talkState[cid] = nil
          selfSay("Goodbye!", cid)
          closeShopWindow(cid)
          removeFocus(cid, focuses)
          lookAtFocus(focuses)
       
    else
         for _,v in pairs(Messages) do
            if v.Key == string.lower(msg) then
               selfSay(v.Reply, cid)
            end
         end
    end
end

function onThink()
    for _, focus in pairs(focuses) do
        if not isCreature(focus) then
         removeFocus(focus, focuses)
        else
         local distance = getDistanceTo(focus) or 5
            if distance > 4 then
                selfSay("HOW DARE YOU LEAVE WITHOUT SAYING GOOD BYE!? THUNDERST... kidding haha, good luck traveller!", focus)
                closeShopWindow(focus)
                removeFocus(focus, focuses)
            end
        end
    end
    lookAtFocus(focuses)
end


-- Second Mode --

Code:
-- MODE 1 --

local focuses, controlId = {}, 3586

local talkState = {}

function onCreatureDisappear(cid)
    if isFocused(cid, focuses) then
     removeFocus(cid, focuses)
        if isPlayer(cid) then
         closeShopWindow(cid)
        end
    end
end

local Messages = {
      ["name"] = "I'm Joe.",
      ["job"] = "I'm a merchant.",
}


function onCreatureSay(cid, type, msg)
 local travelWindow = {}
    if msgcontains(msg, "hi", "hello") and not isFocused(cid, focuses) and getDistanceToCreature(cid) < 4 then
       addFocus(cid, focuses)
       selfSay("Hiho " .. getCreatureName(cid) .. ", i'm the test npc.", cid)
     
    elseif msgcontains(msg, "bye") then
          talkState[cid] = nil
          selfSay("Goodbye!", cid)
          closeShopWindow(cid)
          removeFocus(cid, focuses)
          lookAtFocus(focuses)
       
    elseif Messages[string.lower(msg)] then
               selfSay(Messages[string.lower(msg)], cid)       
    end
end

function onThink()
    for _, focus in pairs(focuses) do
        if not isCreature(focus) then
         removeFocus(focus, focuses)
        else
         local distance = getDistanceTo(focus) or 5
            if distance > 4 then
                selfSay("HOW DARE YOU LEAVE WITHOUT SAYING GOOD BYE!? THUNDERST... kidding haha, good luck traveller!", focus)
                closeShopWindow(focus)
                removeFocus(focus, focuses)
            end
        end
    end
    lookAtFocus(focuses)
end
 
There are two ways of doing that, here's the full script of an example NPC using both modes:

-- First Mode --

Code:
-- MODE 1 --

local focuses, controlId = {}, 3586

local talkState = {}

function onCreatureDisappear(cid)
    if isFocused(cid, focuses) then
     removeFocus(cid, focuses)
        if isPlayer(cid) then
         closeShopWindow(cid)
        end
    end
end

local Messages = {
     {Key = "name", Reply = "I'm Joe."},
     {Key = "job", Reply = "I'm a merchant."}
}


function onCreatureSay(cid, type, msg)
 local travelWindow = {}
    if msgcontains(msg, "hi", "hello") and not isFocused(cid, focuses) and getDistanceToCreature(cid) < 4 then
       addFocus(cid, focuses)
       selfSay("Hiho " .. getCreatureName(cid) .. ", i'm the test npc.", cid)
     
    elseif msgcontains(msg, "bye") then
          talkState[cid] = nil
          selfSay("Goodbye!", cid)
          closeShopWindow(cid)
          removeFocus(cid, focuses)
          lookAtFocus(focuses)
       
    else
         for _,v in pairs(Messages) do
            if v.Key == string.lower(msg) then
               selfSay(v.Reply, cid)
            end
         end
    end
end

function onThink()
    for _, focus in pairs(focuses) do
        if not isCreature(focus) then
         removeFocus(focus, focuses)
        else
         local distance = getDistanceTo(focus) or 5
            if distance > 4 then
                selfSay("HOW DARE YOU LEAVE WITHOUT SAYING GOOD BYE!? THUNDERST... kidding haha, good luck traveller!", focus)
                closeShopWindow(focus)
                removeFocus(focus, focuses)
            end
        end
    end
    lookAtFocus(focuses)
end


-- Second Mode --

Code:
-- MODE 1 --

local focuses, controlId = {}, 3586

local talkState = {}

function onCreatureDisappear(cid)
    if isFocused(cid, focuses) then
     removeFocus(cid, focuses)
        if isPlayer(cid) then
         closeShopWindow(cid)
        end
    end
end

local Messages = {
      ["name"] = "I'm Joe.",
      ["job"] = "I'm a merchant.",
}


function onCreatureSay(cid, type, msg)
 local travelWindow = {}
    if msgcontains(msg, "hi", "hello") and not isFocused(cid, focuses) and getDistanceToCreature(cid) < 4 then
       addFocus(cid, focuses)
       selfSay("Hiho " .. getCreatureName(cid) .. ", i'm the test npc.", cid)
     
    elseif msgcontains(msg, "bye") then
          talkState[cid] = nil
          selfSay("Goodbye!", cid)
          closeShopWindow(cid)
          removeFocus(cid, focuses)
          lookAtFocus(focuses)
       
    elseif Messages[string.lower(msg)] then
               selfSay(Messages[string.lower(msg)], cid)       
    end
end

function onThink()
    for _, focus in pairs(focuses) do
        if not isCreature(focus) then
         removeFocus(focus, focuses)
        else
         local distance = getDistanceTo(focus) or 5
            if distance > 4 then
                selfSay("HOW DARE YOU LEAVE WITHOUT SAYING GOOD BYE!? THUNDERST... kidding haha, good luck traveller!", focus)
                closeShopWindow(focus)
                removeFocus(focus, focuses)
            end
        end
    end
    lookAtFocus(focuses)
end

Thank you i'm using first mode!.
Repp++
 
Back
Top