• 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!

Simple way to remove monster?

Zazeros

Member
Joined
Feb 13, 2012
Messages
64
Reaction score
17
0.4
Hi, I am struggling with some think I know its very easy.

I want a way to remove a creature after some time after it has been created.
I have a npc script where summons a creature, and it adds an event that removes it, but if a player kills the creature, obviously returns an error.

So I tried with creaturescripts:

Lua:
function onThink(creature, interval)
addEvent(function()
if isCreature(cid) then
    doRemoveCreature(getCreatureByName("Demon"))
    end
 end, 4000)
    return true
end

Didn't return any error, but the creature didn't get removed.
I also add the event in the monster xml

How can I make this work?
Thanks.
 
Solution
@Xikini
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 = {}



local function removeCreature(cid)
    if not isCreature(cid) then
        return
    end
    doRemoveCreature(cid)
end

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
 
    local talkUser = NPCHANDLER_CONVBEHAVIOR ==...
When you create the monster, you need to grab it's id like this
Lua:
local monster = doCreateMonster("rat", position)

Then you can create an addEvent and use the monster's cid in it's parameters
Lua:
local function removeCreature(cid)
    if not isCreature(cid) then
        return
    end
    doRemoveCreature(cid)
end

Lua:
-- inside your code where you create the monster
local monster = doCreateMonster("rat", position)
if not isCreature(monster) then
    print("Creature was not created?")
else
    addEvent(removeCreature, 4000, monster)
end
 
@Xikini Sorry, I didn't understand.
The script where summons the monster is a npc.lua
And where removes it, it is a creaturescript
Where do I have to add this?
 
@Xikini Sorry, I didn't understand.
The script where summons the monster is a npc.lua
And where removes it, it is a creaturescript
Where do I have to add this?
You would get rid of the creaturescript, and put the logic into the npc.

It's kinda hard to give you more specific information without seeing the scripts you're using, and what your end goal is, tho
 
@Xikini I think I'm still doing something wrong
I put the Function part in the start of the npc script, and then paste the "-- inside your code where you create the monster" part after, inside the npc script, and returned this error:
[14/5/2022 13:16:57] (luaAddEvent) Callback parameter should be a function
 
@Xikini I think I'm still doing something wrong
I put the Function part in the start of the npc script, and then paste the "-- inside your code where you create the monster" part after, inside the npc script, and returned this error:
[14/5/2022 13:16:57] (luaAddEvent) Callback parameter should be a function
Can you post your code?
 
@Xikini
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 = {}



local function removeCreature(cid)
    if not isCreature(cid) then
        return
    end
    doRemoveCreature(cid)
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)
            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())   
                local position = {x = 848, y = 1106, z = 7}
                local monster = doCreateMonster("rat", position)
if not isCreature(monster) then
    print("Creature was not created?")
else
    addEvent(removeCreature, 4000, monster)
end
                addEvent(doCreateNpc, 4000, "EF westfall", {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())
 
@Xikini
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 = {}



local function removeCreature(cid)
    if not isCreature(cid) then
        return
    end
    doRemoveCreature(cid)
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)
            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())
                local position = {x = 848, y = 1106, z = 7}
                local monster = doCreateMonster("rat", position)
if not isCreature(monster) then
    print("Creature was not created?")
else
    addEvent(removeCreature, 4000, monster)
end
                addEvent(doCreateNpc, 4000, "EF westfall", {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())
Changed a bit and added prints.

Let me know what prints to console.

Lua:
local function removeCreature(cid)
    if not isCreature(cid) then
        print("Creature no longer exists.")
        return
    end
    print("Creature is being removed...")
    doRemoveCreature(cid)
    print("Creature was removed.")
end

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)
                local position = {x = 848, y = 1106, z = 7}
                local monster = doCreateMonster("rat", position)
                if not isCreature(monster) then
                    print("Creature was not created?")
                else
                    print("Creature was spawned successfully.")
                    addEvent(removeCreature, 4000, monster)
                end
                addEvent(doCreateNpc, 4000, "EF westfall", {x = 848, y = 1106, z = 7})
                doRemoveCreature(getNpcCid())
            end
        end
        talkState[talkUser] = 0
    end
   
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
@Xikini Ok, I found something a little annoying.
When the Npc disappears, the conversation never ends, so if I try to talk to another npc, or the same when it spawns, I can't. The only way is saying "bye" first in the npc channel to any npc, and then I can say hi again.
Is there a way to end the conversation when the npc disappears?
 
@Xikini Ok, I found something a little annoying.
When the Npc disappears, the conversation never ends, so if I try to talk to another npc, or the same when it spawns, I can't. The only way is saying "bye" first in the npc channel to any npc, and then I can say hi again.
Is there a way to end the conversation when the npc disappears?
Before you remove the npc, try adding
Lua:
npcHandler:releaseFocus(cid)
 
@Xikini Simple as that. Thank you.
I thought it had something to do with talkState[talkUser], but I have no idea what does it do.
Thank you again!!
Afaik talkState[talkUser] is a simple table.
It would look like this
Lua:
talkState = {
    [talkUser] = 0, -- talkUser is the players creatureId, I imagine
    [creatureId] = number,
}

So it's basically used to just hold reference data, and somewhere in lib it's reset to default 0 or -1 whenever you initialize a conversation with an npc.
 
Back
Top