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

TFS 1.X+ Remove npc in npc script?

Mjmackan

Mapper ~ Writer
Joined
Jul 18, 2009
Messages
1,488
Solutions
18
Reaction score
199
Location
Sweden
Using TFS 1.4.2

Question: Is it possible to get the npc from an npc script, in this case i want to remove the npc from its own npc script but in order to do so I need to somehow get the npc, I tried a lot of combinations, lastly with getSpectator which didn't solve it. Script below. Any ideas would be awesome!
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 MISSION_STORAGE = 7452
local QUEST_COMPLETED = 7453

function greet(cid, type, msg)
    if not npcHandler:isFocused(cid) then return false end

    local player = Player(cid)

    if player:getStorageValue(QUEST_COMPLETED) == 1 then
        npcHandler:say("Sorry for the hickup, excuse me human, you get to buy sanity potion cheaper from now on from me.", cid)
        return true
    end

    if player:getItemCount(14335) > 0 then
        if player:getStorageValue(MISSION_STORAGE) < 2 then
            npcHandler:say("You haven't brought my letter to Koriana yet.", cid)
            return true
        end

        player:removeItem(14335, 1)
        player:setStorageValue(MISSION_STORAGE, 3)

        npcHandler:say("Thank you! Now I must return to my true form...", cid)

        local pos = player:getPosition()

        for i = 1, 40 do
            local effectPos = Position(pos.x + math.random(-6, 6), pos.y + math.random(-6, 6), pos.z)
            effectPos:sendMagicEffect(math.random(10, 50))
        end
        pos:sendMagicEffect(CONST_ME_TELEPORT)
        pos:sendMagicEffect(CONST_ME_FIREWORK_BLUE)
        pos:sendMagicEffect(CONST_ME_FIREWORK_RED)

        local spectators = Game.getSpectators(pos, false, true, 5, 5, 5, 5)
        for _, creature in ipairs(spectators) do
            if creature:isNpc() and creature:getName():lower() == "Toriana [free fairy]" then
                creature:remove()
                break
            end
        end

        addEvent(function()
            Game.createMonster("Toriana", pos)
        end, 1500)

        return true
    end

    if msgcontains(msg, "mission") or msgcontains(msg, "help") or msgcontains(msg, "letter") then
        if player:getStorageValue(MISSION_STORAGE) >= 1 then
            npcHandler:say("You already have my letter. Please bring it to Koriana.", cid)
        else
            player:addItem(14326, 1)
            player:setStorageValue(MISSION_STORAGE, 1)
            npcHandler:say("Thank you! Here is the letter for Koriana. Please bring it to her quickly.", cid)
        end
        return true
    end

    return false
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, greet)
npcHandler:addModule(FocusModule:new())
 
Code:
local npc = Npc(getNpcCid())

You can use it in any npc file. You may even be able to just do what Lessne said but I haven't tested that.
Code:
local npc = Npc()

Do that in any functions that are assigned to an npc callback like creatureSayCallback.
 
Code:
Npc()

Code:
local npc = Npc(getNpcCid())

You can use it in any npc file. You may even be able to just do what Lessne said but I haven't tested that.
Code:
local npc = Npc()

Do that in any functions that are assigned to an npc callback like creatureSayCallback.
Haha it was that easy, thanks both!
 
Back
Top