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

script npc modification

foundation5

New Member
Joined
Oct 26, 2023
Messages
5
Reaction score
0
I have this npc script to travel it is working, I would like to make just one change...

In the case if the player has money in the bag and the player tries to travel it works, however if the player doesn't have money in the bag but has money in the bank it doesn't work.

so basically you would need to make the script check if the player has money in the bank and if so he can travel normally.

script
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)
    function lowerTable(tabela) -- by vodkart
        local tab = {}
        for var, ret in next, tabela do
            tab[var:lower()] = ret
        end
        return tab
    end
    if not npcHandler:isFocused(cid) then
        return false
    end
    local talkUser,msg = NPCHANDLER_CONVbehavior == CONVERSATION_DEFAULT and 0 or cid, msg:lower()
    local viagens = {
                ['Dark City'] = {premium = false, level = 0, cost = 200, destination = {x=766, y=1090, z=6}},

    }
    local magician_pos = {x=1249, y=783, z=6}
    local travel = lowerTable(viagens)
    if travel[msg] then
        local var = travel[msg]
        if var.level > 0 and getPlayerLevel(cid) < var.level then
            npcHandler:say("Sorry but you must be level ".. var.level .." to travel to this place.", cid) return true
        elseif var.premium and not isPremium(cid) then
            npcHandler:say("Sorry, to go to "..msg.." it is necessary to be VIP.", cid) return true     
        elseif not doPlayerRemoveMoney(cid, var.cost) then   
            npcHandler:say("Sorry, you don't have enough GPS. Is required ".. var.cost .."gps to go to "..msg..".", cid) return true
        end
        if var.chance ~= nil and var.chance > 0 then
            if getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).itemid == 10219 then
                mychance = 100
            else
                mychance = var.chance
            end
        end
        npcHandler:say("Bon voyage!", cid)           
        npcHandler:releaseFocus(cid)
        if var.chance ~= nil and var.chance > 0 then
            if mychance > math.random(1, 100) then
                doTeleportThing(cid, garath_pos)
                npcHandler:say("The boat lost its way and stopped in Magician, Careful!", cid)
            else
                doTeleportThing(cid, var.destination)
            end
        else
            doTeleportThing(cid, var.destination)
        end     
        doSendMagicEffect(getPlayerPosition(cid), CONST_ME_TELEPORT)
    else
        selfSay("I don't sail to this city...", cid)
    end
    return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

modules.lua
function Player.removeMoneyNpc(self, amount)
local moneyCount = self:getMoney()
local bankCount = self:getBankBalance()
if amount > moneyCount + bankCount then
return false
end

self:removeMoney(math.min(amount, moneyCount))
if amount > moneyCount then
local newBalance = bankCount - math.max(amount - moneyCount, 0)
self:setBankBalance(newBalance)
if moneyCount == 0 then
self:sendTextMessage(MESSAGE_INFO_DESCR, ("You paid %d gps from your bank account. Your balance went from %d to %d gps."):format(amount, bankCount, newBalance))
else
self:sendTextMessage(MESSAGE_INFO_DESCR, ("You paid %d from inventory and %d gold from bank account. Your account balance went from %d to %d gps."):format(moneyCount, amount - moneyCount, bankCount, newBalance))
end
end
return true
end
 
try.

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

local magician_pos = {x=1249, y=783, z=6}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    
    local travelDestinations = {
        ['dark city'] = {premium = false, level = 0, cost = 200, destination = {x=766, y=1090, z=6}, chance = 50}
    }

    local player = Player(cid)
    local travel = travelDestinations[msg:lower()]
    
    if travel then
        if travel.level > 0 and player:getLevel() < travel.level then
            npcHandler:say("Sorry but you must be level " .. travel.level .. " to travel to this place.", cid)
        elseif travel.premium and not player:isPremium() then
            npcHandler:say("Sorry, to go to " .. msg .. " it is necessary to be VIP.", cid)
        elseif not player:removeMoney(travel.cost) then
            npcHandler:say("Sorry, you don't have enough gold. It costs " .. travel.cost .. " gold to go to " .. msg .. ".", cid)
        else
            local mychance = travel.chance or 100
            if player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 10219 then
                mychance = 100
            end
            
            if math.random(1, 100) <= mychance then
                local pos = travel.destination
                local magicEffect = CONST_ME_TELEPORT
                Position(pos):sendMagicEffect(magicEffect)
                doTeleportThing(cid, pos)
                npcHandler:say("Bon voyage!", cid)
            else
                local pos = magician_pos
                local magicEffect = CONST_ME_TELEPORT
                Position(pos):sendMagicEffect(magicEffect)
                doTeleportThing(cid, pos)
                npcHandler:say("The boat lost its way and stopped in Magician, Careful!", cid)
            end
        end
    else
        npcHandler:say("I don't sail to this city...", cid)
    end
    
    npcHandler:releaseFocus(cid)
    return true
end

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

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

local magician_pos = {x=1249, y=783, z=6}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
   
    local travelDestinations = {
        ['dark city'] = {premium = false, level = 0, cost = 200, destination = {x=766, y=1090, z=6}, chance = 50}
    }

    local player = Player(cid)
    local travel = travelDestinations[msg:lower()]
   
    if travel then
        if travel.level > 0 and player:getLevel() < travel.level then
            npcHandler:say("Sorry but you must be level " .. travel.level .. " to travel to this place.", cid)
        elseif travel.premium and not player:isPremium() then
            npcHandler:say("Sorry, to go to " .. msg .. " it is necessary to be VIP.", cid)
        elseif not player:removeMoney(travel.cost) then
            npcHandler:say("Sorry, you don't have enough gold. It costs " .. travel.cost .. " gold to go to " .. msg .. ".", cid)
        else
            local mychance = travel.chance or 100
            if player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 10219 then
                mychance = 100
            end
           
            if math.random(1, 100) <= mychance then
                local pos = travel.destination
                local magicEffect = CONST_ME_TELEPORT
                Position(pos):sendMagicEffect(magicEffect)
                doTeleportThing(cid, pos)
                npcHandler:say("Bon voyage!", cid)
            else
                local pos = magician_pos
                local magicEffect = CONST_ME_TELEPORT
                Position(pos):sendMagicEffect(magicEffect)
                doTeleportThing(cid, pos)
                npcHandler:say("The boat lost its way and stopped in Magician, Careful!", cid)
            end
        end
    else
        npcHandler:say("I don't sail to this city...", cid)
    end
   
    npcHandler:releaseFocus(cid)
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
it didn't work bro
Lua Script Error: [Npc interface]
data/npc/scripts/captain claudiao.lua:eek:nCreatureSay
data/npc/scripts/captain claudiao.lua:45: attempt to index a nil value
stack traceback:
[C]: in function '__index'
data/npc/scripts/captain claudiao.lua:45: in function 'callback'
data/npc/lib/npcsystem/npchandler.lua:411: in function 'onCreatureSay'
data/npc/scripts/captain claudiao.lua:15: in function <data/npc/scripts/captain claudiao.lua:14>
 

I just tested it and it worked ok...

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

local magician_pos = {x=1249, y=783, z=6}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    
    local travelDestinations = {
        ['dark city'] = {premium = false, level = 0, cost = 200, destination = {x=766, y=1090, z=6}, chance = 50}
    }

    local player = Player(cid)
    local travel = travelDestinations[msg:lower()]
    
    if travel then
        if travel.level > 0 and player:getLevel() < travel.level then
            npcHandler:say("Sorry, you must be level " .. travel.level .. " to travel to this place.", cid)
        elseif travel.premium and not player:isPremium() then
            npcHandler:say("Sorry, to go to " .. msg .. " it is necessary to be VIP.", cid)
        else
            local travelCost = travel.cost
            local success = player:removeTotalMoney(travelCost)
            if success then
                local remainingBalance = player:getBankBalance()
                local message = ""
                if remainingBalance > 0 then
                    message = ("You paid %d gold for the travel service for %s. Your current balance is %d gold in the bank."):format(travelCost, msg, remainingBalance)
                else
                    message = ("You paid %d gold for the travel service to %s. Your bank balance is empty."):format(travelCost, msg)
                end
                player:sendTextMessage(MESSAGE_INFO_DESCR, message)

                local mychance = travel.chance or 100
                if player:getSlotItem(CONST_SLOT_NECKLACE) and player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 10219 then
                    mychance = 100
                end

                if math.random(1, 100) <= mychance then
                    local pos = travel.destination
                    local magicEffect = CONST_ME_TELEPORT
                    Position(pos):sendMagicEffect(magicEffect)
                    doTeleportThing(cid, pos)
                    npcHandler:say("Bon voyage!", cid)
                else
                    local pos = magician_pos
                    local magicEffect = CONST_ME_TELEPORT
                    Position(pos):sendMagicEffect(magicEffect)
                    doTeleportThing(cid, pos)
                    npcHandler:say("The boat lost its way and stopped in Magician. Careful!", cid)
                end
            else
                npcHandler:say("You don't have enough money to travel to " .. msg .. ".", cid)
            end
        end
    else
        npcHandler:say("I don't sail to this city...", cid)
    end
    
    npcHandler:releaseFocus(cid)
    return true
end

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

I just tested it and it worked ok...

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

local magician_pos = {x=1249, y=783, z=6}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
   
    local travelDestinations = {
        ['dark city'] = {premium = false, level = 0, cost = 200, destination = {x=766, y=1090, z=6}, chance = 50}
    }

    local player = Player(cid)
    local travel = travelDestinations[msg:lower()]
   
    if travel then
        if travel.level > 0 and player:getLevel() < travel.level then
            npcHandler:say("Sorry, you must be level " .. travel.level .. " to travel to this place.", cid)
        elseif travel.premium and not player:isPremium() then
            npcHandler:say("Sorry, to go to " .. msg .. " it is necessary to be VIP.", cid)
        else
            local travelCost = travel.cost
            local success = player:removeTotalMoney(travelCost)
            if success then
                local remainingBalance = player:getBankBalance()
                local message = ""
                if remainingBalance > 0 then
                    message = ("You paid %d gold for the travel service for %s. Your current balance is %d gold in the bank."):format(travelCost, msg, remainingBalance)
                else
                    message = ("You paid %d gold for the travel service to %s. Your bank balance is empty."):format(travelCost, msg)
                end
                player:sendTextMessage(MESSAGE_INFO_DESCR, message)

                local mychance = travel.chance or 100
                if player:getSlotItem(CONST_SLOT_NECKLACE) and player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 10219 then
                    mychance = 100
                end

                if math.random(1, 100) <= mychance then
                    local pos = travel.destination
                    local magicEffect = CONST_ME_TELEPORT
                    Position(pos):sendMagicEffect(magicEffect)
                    doTeleportThing(cid, pos)
                    npcHandler:say("Bon voyage!", cid)
                else
                    local pos = magician_pos
                    local magicEffect = CONST_ME_TELEPORT
                    Position(pos):sendMagicEffect(magicEffect)
                    doTeleportThing(cid, pos)
                    npcHandler:say("The boat lost its way and stopped in Magician. Careful!", cid)
                end
            else
                npcHandler:say("You don't have enough money to travel to " .. msg .. ".", cid)
            end
        end
    else
        npcHandler:say("I don't sail to this city...", cid)
    end
   
    npcHandler:releaseFocus(cid)
    return true
end

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

I just tested it and it worked ok...

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

local magician_pos = {x=1249, y=783, z=6}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
   
    local travelDestinations = {
        ['dark city'] = {premium = false, level = 0, cost = 200, destination = {x=766, y=1090, z=6}, chance = 50}
    }

    local player = Player(cid)
    local travel = travelDestinations[msg:lower()]
   
    if travel then
        if travel.level > 0 and player:getLevel() < travel.level then
            npcHandler:say("Sorry, you must be level " .. travel.level .. " to travel to this place.", cid)
        elseif travel.premium and not player:isPremium() then
            npcHandler:say("Sorry, to go to " .. msg .. " it is necessary to be VIP.", cid)
        else
            local travelCost = travel.cost
            local success = player:removeTotalMoney(travelCost)
            if success then
                local remainingBalance = player:getBankBalance()
                local message = ""
                if remainingBalance > 0 then
                    message = ("You paid %d gold for the travel service for %s. Your current balance is %d gold in the bank."):format(travelCost, msg, remainingBalance)
                else
                    message = ("You paid %d gold for the travel service to %s. Your bank balance is empty."):format(travelCost, msg)
                end
                player:sendTextMessage(MESSAGE_INFO_DESCR, message)

                local mychance = travel.chance or 100
                if player:getSlotItem(CONST_SLOT_NECKLACE) and player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 10219 then
                    mychance = 100
                end

                if math.random(1, 100) <= mychance then
                    local pos = travel.destination
                    local magicEffect = CONST_ME_TELEPORT
                    Position(pos):sendMagicEffect(magicEffect)
                    doTeleportThing(cid, pos)
                    npcHandler:say("Bon voyage!", cid)
                else
                    local pos = magician_pos
                    local magicEffect = CONST_ME_TELEPORT
                    Position(pos):sendMagicEffect(magicEffect)
                    doTeleportThing(cid, pos)
                    npcHandler:say("The boat lost its way and stopped in Magician. Careful!", cid)
                end
            else
                npcHandler:say("You don't have enough money to travel to " .. msg .. ".", cid)
            end
        end
    else
        npcHandler:say("I don't sail to this city...", cid)
    end
   
    npcHandler:releaseFocus(cid)
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
so I even have some NPCs like this, however the script above has some details if the player has a ring equipped he goes to another city and the player doesn't need to say yes to travel
 
Something like? I tested without any items, just with money in the bank, and I was taken to the Dark City, everything is fine. And in another account that has no money either in the bank or in the backpack, just equipped with the ring, I was taken to a special place. As for how it works, just talk to the NPC using just "hi" and "yes", and you're done.
just change the item id.

local requiredRingID = 7708

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

local magicianPos = {x=1249, y=783, z=6}
local darkCityPos = {x=766, y=1090, z=6}
local requiredRingID = 7708
local requiredLevel = 0

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 player = Player(cid)
   
    local ringItem = player:getSlotItem(CONST_SLOT_RING)
    if ringItem and ringItem:getId() == requiredRingID and player:getLevel() >= requiredLevel and player:isPremium() then
        doTeleportThing(cid, magicianPos)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have been teleported to a special location.")
        return true
    end

    if player:getLevel() >= requiredLevel and player:isPremium() and player:removeTotalMoney(200) then
        doTeleportThing(cid, darkCityPos)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You paid 200 gold for the travel service to Dark City.")
        return true
    end

    if player:isPremium() then
        if player:getLevel() >= requiredLevel then
            npcHandler:say("Sorry, you do not have enough money or the required item.", cid)
        else
            npcHandler:say("Sorry, you do not meet the level requirement.", cid)
        end
    else
        npcHandler:say("Sorry, you are not a VIP member.", cid)
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited:
Back
Top