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

Quest Npc Script. TFS 1.4.2/ otclient redemption

eardrums

Member
Joined
May 27, 2010
Messages
94
Solutions
1
Reaction score
10
Hello everyone,

so I'm working on my first quest npc and so far I've managed to get pretty far. To be honest though I find it really difficult to keep the script organized and there is a lot i still dont understand. Can someone take a look at the script for me and help me out to organize it. Writing a new one that's easier to work with. Thanks in advance!

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 function greetCallback(cid)
    local player = Player(cid)
    if player:getStorageValue(30001) == -1 then
        npcHandler:setMessage(MESSAGE_GREET, "Oh thank god I ran into you adventurer. can you help me?")
    elseif player:getStorageValue(30001) == 1 then
        npcHandler:setMessage(MESSAGE_GREET, "Did you activate the generator in the west control room? ")
    elseif player:getStorageValue(30001) == 2 then
        npcHandler:setMessage(MESSAGE_GREET, "Did you activate the generator in the west control room? ")
    elseif player:getStorageValue(30001) == 3 then
        npcHandler:setMessage(MESSAGE_GREET, "Did you activate the generator in the west control room? ")
    elseif player:getStorageValue(30001) == 4 then
        npcHandler:setMessage(MESSAGE_GREET, "Did you find out what condition they are in? ")
    end
    return true
end

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

    local player = Player(cid)

    if msgcontains(msg, "yes") then
        local storage = player:getStorageValue(30001)
        if storage == -1 and
        npcHandler.topic[cid] == 0 then
            npcHandler:say({
                "I'm afraid we have a problem. Two people have gotten sick, and we think it's because the water from the sewers is contaminated. ...",
                "The sewers provide the water source for the town well, so everyone in town is at risk. ...",
                "We need your help to find the source of the contamination and stop it before it's too late. ...",
                "I would go down myself, but the rats have gotten more aggressive down there lately.",
                "I'm not equipped to deal with them, and I'm afraid that if I go down there alone, I'll end up getting hurt or killed. ...",
                "That's why I'm asking you for help. We need someone who's brave and resourceful enough to go down into the sewers and find the source of the contamination. ...",
                "Are you willing to go down into the deeper sewers and investigate the issue?"
            }, cid)

            npcHandler.topic[cid] = 1
            return true
        elseif storage == 1 then
            npcHandler:say("Well, the generator is still not on, so either you're lying or you did something wrong. Either way, I'm not going to waste any more time with you. I can just find someone else to do the job.", cid)
            return true
        elseif storage == 2 then
            npcHandler:say("Well, the generator is still not on, so either you're lying or you did something wrong. Either way, I'm not going to waste any more time with you. I can just find someone else to do the job.", cid)
            return true
        elseif storage == 3 then
            npcHandler:say({
            "Well done! You bought us sometime to find out where the contamination came form. ...",
            "I need you to do me a favor before we start looking for the source. I need you to go check on the two people who got sick. ...",
            "They live with Grandpa Joe. Please find out what condition they are in for me."
            }, cid)
            player:setStorageValue(30001, 4)
            return true
        elseif storage == 4 then
            npcHandler:say("You are lying! Grandpa Joe told me that nobody came to help him. Get out of my sight!", cid)
            return true
            
        end
    
    if msgcontains(msg, "yes") and npcHandler.topic[cid] == 1 then
    
        npcHandler:say({
        "Thank you for agreeing to help. I know this is a dangerous task, but it's important that we find the source of the contamination and stop it. ...",
        "The first thing you need to do is go to the east control room and switch off the water flow. ...",
        "This is the current water supply that's being used, and I need to stop the contaminated water from flowing before other people get sick. ...",
        "Once you've switched off the water flow, go to the west control room and turn on the generator. This will start the backup water supply, which is not contaminated. Good luck!"
        }, cid)
        
        player:setStorageValue(30001, 1)
        npcHandler.topic[cid] = 2
        end
    
    elseif msgcontains(msg, "coming up 2") and npcHandler.topic[cid] == 2 then
        player:setStorageValue(30001, 1)
        npcHandler:say("still", cid)
        npcHandler.topic[cid] = 0
    
    elseif npcHandler.topic[cid] > 0 then
        npcHandler:say("still", cid)
        npcHandler.topic[cid] = 0
    
    end
    return true
end

local function onAddFocus(cid)
    npcHandler.topic[cid] = 0
end

local function onReleaseFocus(cid)
    npcHandler.topic[cid] = 0
end

npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Lua:
-- Include the necessary libraries
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

-- Callback functions
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

-- Greeting callback function
local function greetCallback(cid)
    local player = Player(cid)
    local storage = player:getStorageValue(30001)

    if storage == -1 then
        npcHandler:setMessage(MESSAGE_GREET, "Oh, thank god I ran into you, adventurer. Can you help me?")
    elseif storage <= 3 then
        npcHandler:setMessage(MESSAGE_GREET, "Did you activate the generator in the west control room?")
    elseif storage == 4 then
        npcHandler:setMessage(MESSAGE_GREET, "Did you find out what condition they are in?")
    end

    return true
end

-- Dialogue callback function
local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)
    local storage = player:getStorageValue(30001)
    local npcTopic = npcHandler.topic[cid]

    if msgcontains(msg, "yes") then
        if storage == -1 and npcTopic == 0 then
            -- First encounter and mission invitation
            npcHandler:say({
                "I'm afraid we have a problem. Two people have fallen sick, and we suspect the sewer water is contaminated. ...",
                "The sewers supply water to the town well, so everyone is at risk. ...",
                "We need your help to locate the contamination source and stop it before it's too late. ...",
                "I would go down myself, but the rats have become more aggressive lately.",
                "I lack the equipment to handle them, and I fear that going down alone might result in injury or worse. ...",
                "That's why I'm asking for your help. We need someone brave and resourceful enough to venture into the sewers and find the source of the contamination. ...",
                "Are you willing to explore the deeper sewers and investigate the issue?"
            }, cid)
            npcHandler.topic[cid] = 1
            return true
        elseif storage == 1 or storage == 2 then
            -- Failure in the first part of the mission
            npcHandler:say("Well, the generator is still not on, so either you're lying or you did something wrong. Either way, I won't waste more time with you. I can find someone else for the job.", cid)
            return true
        elseif storage == 3 then
            -- Instructions after activating the generator
            npcHandler:say({
                "Well done! You've given us some time to investigate the contamination source. ...",
                "Before we proceed with the investigation, I need a favor. Check on the two sick individuals for me. ...",
                "They live with Grandpa Joe. Please assess their condition and report back to me."
            }, cid)
            player:setStorageValue(30001, 4)
            return true
        elseif storage == 4 then
            -- Player tries to deceive the NPC
            npcHandler:say("You're lying! Grandpa Joe told me that no one came to help him. Leave my sight!", cid)
            return true
        end
if msgcontains(msg, "yes") and npcTopic == 1 then
    -- Accepting the mission
    npcHandler:say({
        "Thank you for agreeing to help. I know this is a dangerous task, but it's important that we find the source of the contamination and put a stop to it. ...",
        "Your first task is to go to the east control room and deactivate the water flow. ...",
        "The current water supply comes from there, and I need to halt the spread of the contaminated water before more people get sick. ...",
        "Once you've shut down the water flow, proceed to the west control room and activate the generator. This will initiate the backup water supply, which is uncontaminated. Good luck!"
    }, cid)
    player:setStorageValue(30001, 1)
    npcHandler.topic[cid] = 2
elseif msgcontains(msg, "coming up 2") and npcTopic == 2 then
    -- Player says "coming up 2" to finish the mission
    player:setStorageValue(30001, 2) -- Storage value correction here
    npcHandler:say("You've completed your task. Well done!", cid)
    npcHandler.topic[cid] = 0
else
    -- Default response message
    npcHandler:say("I'm not sure what you mean.", cid)
    npcHandler.topic[cid] = 0
end


    return true
end

-- Focus functions
local function onAddFocus(cid)
    npcHandler.topic[cid] = 0
end

local function onReleaseFocus(cid)
    npcHandler.topic[cid] = 0
end

-- Set the callbacks
npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Back
Top