• 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!
  • If you're using Gesior 2012 or MyAAC, please review this thread for information about a serious security vulnerability and a fix.

DoRemoveCreature and DoCreateNPC

Zazeros

Member
Joined
Feb 13, 2012
Messages
56
Reaction score
17
0.4

Hello guys, I'm trying to do a NPC something like this:
Player says something to him, there is a chance of the npc desappears and, if he desappears, he will appears again later.

Until now, I made this:
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
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



function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid



if(msgcontains(msg, 'help')) then
if getPlayerStorageValue(cid,877541) == -1 then
selfSay('Leave me alone.', cid)
else
selfSay('{yes}?', cid)
end
end
    if(msgcontains(msg, 'yes')) then
    rand = math.random(0, 100)
if rand >= 85 then
selfSay('1', cid)
else
selfSay('2', cid)
doRemoveCreature(getCreatureByName('Test NPC'))

addEvent(function()
local pos1 = {x = 848, y = 1106, z = 7}
                    doCreateNpc('Test NPC', pos1)
             end, 4000)
           
        return true
        end
       
    end
    end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())


But he does not get removed if math random is right and nether get created again, and this is how I know:

[22/4/2022 14:11:25] [Warning - Npc::createNpc] Cannot find npc with name: Test NPC.
[22/4/2022 14:11:26] [Error - NpcScript Interface]
[22/4/2022 14:11:26] In a timer event called from:
[22/4/2022 14:11:26] data/npc/scripts/EF westfall sem teto.lua:eek:nCreatureSay
[22/4/2022 14:11:26] Description:
[22/4/2022 14:11:26] (luaDoCreateNpc) Npc with name 'Test NPC' not found

What can I do?
 
Solution
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 talkState = {}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    if msgcontains(msg, "help") then
        if getPlayerStorageValue(cid,877541) == -1 then...

mano368

Well-Known Member
Joined
Sep 2, 2011
Messages
210
Solutions
25
Reaction score
84
Location
Brazil
Lua:
doRemoveCreature(getCreatureByName('Test NPC'))
To

Code:
doRemoveCreature(getCreaturePosition(getCreatureByName('Test NPC')))

And about the createnpc, check the name of the npc, if it is spelled correctly
 
Last edited:

mano368

Well-Known Member
Joined
Sep 2, 2011
Messages
210
Solutions
25
Reaction score
84
Location
Brazil
@mano368 Same error man.
Also, I checked the name and it is spelled correctly
when you create a npc, you write its xml file name and not its own name that is inside the file, for example, captain.xml and captain01.xml, but within the game both will have the name captain, so make sure when creating the npc, that it is calling the name of the correct xml file(captain or captain01)

as for the removal, I believe that the function to fetch the location of the npc is missing, which I don't remember now.

Edit:

Try this

Lua:
doRemoveCreature(getNpcPos().uid)
 
Last edited:

ond

Veteran OT User
Joined
Mar 24, 2008
Messages
2,756
Solutions
24
Reaction score
473
Location
Sweden
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 talkState = {}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    if msgcontains(msg, "help") then
        if getPlayerStorageValue(cid,877541) == -1 then
            selfSay("Leave me alone.", cid)
        else
            selfSay("{yes}?", cid)
            talkState[talkUser] = 1
        end
    elseif talkState[talkUser] == 1 then     
        if msgcontains(msg, "yes") then
            if math.random(0, 100) >= 85 then
                selfSay("1", cid)
            else
                selfSay("2", cid)
                doRemoveCreature(getNpcCid())   
                addEvent(doCreateNpc, 4000, "Test NPC", {x = 848, y = 1106, z = 7})
            end
        end
        talkState[talkUser] = 0
    end
    
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
OP
OP
Z

Zazeros

Member
Joined
Feb 13, 2012
Messages
56
Reaction score
17
@mano368 Thank you, but I tested his first and it kinda worked
Just to be sure, I am ctrl+c ctrl+v in his xml, here:
Lua:
<npc name="Test NPC" script="EF westfall sem teto.lua" walkinterval="0" floorchange="0" lookdir="3" >


@ond The npc got removed, but returned this error

(luaDoCreateNpc) Npc with name 'Test NPC' not found
And it didn't get created again.
 

chucky91

Well-Known Member
Joined
Apr 8, 2010
Messages
147
Solutions
8
Reaction score
74
@mano368 Thank you, but I tested his first and it kinda worked
Just to be sure, I am ctrl+c ctrl+v in his xml, here:
Lua:
<npc name="Test NPC" script="EF westfall sem teto.lua" walkinterval="0" floorchange="0" lookdir="3" >


@ond The npc got removed, but returned this error

(luaDoCreateNpc) Npc with name 'Test NPC' not found
And it didn't get created again.
Look at your talkaction in the GM command in the part that summons NPCS and copy the code
 

mano368

Well-Known Member
Joined
Sep 2, 2011
Messages
210
Solutions
25
Reaction score
84
Location
Brazil
@mano368 Thank you, but I tested his first and it kinda worked
Just to be sure, I am ctrl+c ctrl+v in his xml, here:
Lua:
<npc name="Test NPC" script="EF westfall sem teto.lua" walkinterval="0" floorchange="0" lookdir="3" >


@ond The npc got removed, but returned this error

(luaDoCreateNpc) Npc with name 'Test NPC' not found
And it didn't get created again.
as i told you before, copy the xml name file, not what is inside.

inside the name is Test NPC, but THE FILE NAME can be ABRACADABRA, so, when summoning a npc, you use the xml file name, not NPC NAME.
 
OP
OP
Z

Zazeros

Member
Joined
Feb 13, 2012
Messages
56
Reaction score
17
@chucky91 Yeah, its doCreateNpc also it didn't work, so maybe my TFS is bugged...

@mano368 I tried both names, and it didn't work, but perhaps is my TFS
Post automatically merged:

Actually, my /n does work. If I type the name, does not work, but if I copy ctrl c in the xml, it works. Have no idea why, but thank you guys, for the patience
 
Last edited:

mano368

Well-Known Member
Joined
Sep 2, 2011
Messages
210
Solutions
25
Reaction score
84
Location
Brazil
@chucky91 Yeah, its doCreateNpc also it didn't work, so maybe my TFS is bugged...

@mano368 I tried both names, and it didn't work, but perhaps is my TFS
Post automatically merged:

Actually, my /n does work. If I type the name, does not work, but if I copy ctrl c in the xml, it works. Have no idea why, but thank you guys, for the patience
Low case and upper case?

I was wondering if addevent work after the "person" who trigger the event desappear(like logout).

As a alternativa method, you can teleport your npc to a place and addevent to teleport him back:
Teleport z-1(disappear)
Teleport z+1(back)
 
Last edited:
Top