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

Lua [TFS 0.X] NPC - Correct doRemoveMoney

potinho

Advanced OT User
Joined
Oct 11, 2009
Messages
1,403
Solutions
17
Reaction score
151
Location
Brazil
Good evening guys, how are you? I have this NPC that I copied from Ralke to sell some items with charge, but he takes the item and its quantity (2160 for crystal coin and 2148 for platinum) and its real value is not included. As I added a new currency in the game, I would like to adjust this NPC to use the correct function of removing money. Could you help me to fix it?

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

item = 'You do not have the required items.'
done = 'Here you are.'

function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
 
        if msgcontains(msg, 'offer') then
        selfSay('Here you can buy the following training items: {training sword}, {training axe}, {training club}, {training bow}, {training rod}, and {training wand}.', cid)

            elseif msgcontains(msg, 'training sword') or msgcontains(msg, 'sword') then
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training sword}?', cid)
                talk_state = 1
            else
                selfSay('I need 4,000,000 gold coins to give you the {training sword}. Come back when you have them.', cid)
                talk_state = 0
            end

            elseif msgcontains(msg, 'yes') and talk_state == 1 then
            talk_state = 0
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
            if doPlayerRemoveItem(cid,2160, 400) or doPlayerRemoveItem(cid,2152, 40000) == TRUE then
                            selfSay(done, cid)
local xx = doPlayerAddItem(cid, 5758, 1)
doItemSetAttribute(xx, "charges", 100)
            end
            else
                selfSay(item, cid)
            end
-- axe ---
            elseif msgcontains(msg, 'training axe') or msgcontains(msg, 'axe') then
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training axe}?', cid)
                talk_state = 2
            else
                selfSay('I need 4,000,000 gold coins to give you the {training axe}. Come back when you have them.', cid)
                talk_state = 0
            end

            elseif msgcontains(msg, 'yes') and talk_state == 2 then
            talk_state = 0
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
            if doPlayerRemoveItem(cid,2160, 400) or doPlayerRemoveItem(cid,2152, 40000) == TRUE then
                            selfSay(done, cid)
local xa = doPlayerAddItem(cid, 5759, 1)
doItemSetAttribute(xa, "charges", 100)
            end
            else
                selfSay(item, cid)
            end
            -- club---
            elseif msgcontains(msg, 'training club') or msgcontains(msg, 'club') then
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training club}?', cid)
                talk_state = 3
            else
                selfSay('I need 4,000,000 gold coins to give you the {training club}. Come back when you have them.', cid)
                talk_state = 0
            end

            elseif msgcontains(msg, 'yes') and talk_state == 3 then
            talk_state = 0
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
            if doPlayerRemoveItem(cid,2160, 400) or doPlayerRemoveItem(cid,2152, 40000) == TRUE then
                            selfSay(done, cid)
local xb = doPlayerAddItem(cid, 5760, 1)
doItemSetAttribute(xb, "charges", 100)
            end
            else
                selfSay(item, cid)
            end
-- pally--
            elseif msgcontains(msg, 'training bow') or msgcontains(msg, 'bow') then
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training bow}?', cid)
                talk_state = 4
            else
                selfSay('I need 4,000,000 gold coins to give you the {training bow}. Come back when you have them.', cid)
                talk_state = 0
            end

            elseif msgcontains(msg, 'yes') and talk_state == 4 then
            talk_state = 0
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
            if doPlayerRemoveItem(cid,2160, 400) or doPlayerRemoveItem(cid,2152, 40000) == TRUE then
                            selfSay(done, cid)
local xc = doPlayerAddItem(cid, 5761, 1)
doItemSetAttribute(xc, "charges", 100)
            end
            else
                selfSay(item, cid)
            end
            --druid--
            elseif msgcontains(msg, 'training rod') or msgcontains(msg, 'rod') then
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training rod}?', cid)
                talk_state = 5
            else
                selfSay('I need 4,000,000 gold coins to give you the {training rod}. Come back when you have them.', cid)
                talk_state = 0
            end

            elseif msgcontains(msg, 'yes') and talk_state == 5 then
            talk_state = 0
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
            if doPlayerRemoveItem(cid,2160, 400) or doPlayerRemoveItem(cid,2152, 40000) == TRUE then
                            selfSay(done, cid)
local xd = doPlayerAddItem(cid, 5756, 1)
doItemSetAttribute(xd, "charges", 100)
            end
            else
                selfSay(item, cid)
            end
            --- sorcerer--

            elseif msgcontains(msg, 'training wand') or msgcontains(msg, 'wand') then
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training wand}?', cid)
                talk_state = 6
            else
                selfSay('I need 4,000,000 gold coins to give you the {training wand}. Come back when you have them.', cid)
                talk_state = 0
            end

            elseif msgcontains(msg, 'yes') and talk_state == 6 then
            talk_state = 0
            if getPlayerItemCount(cid,2160) >= 400 or getPlayerItemCount(cid,2152) >= 40000 then
            if doPlayerRemoveItem(cid,2160, 400) or doPlayerRemoveItem(cid,2152, 40000) == TRUE then
                            selfSay(done, cid)
local xf = doPlayerAddItem(cid, 5757, 1)
doItemSetAttribute(xf, "charges", 100)
            end
            else
                selfSay(item, cid)
            end
    return true
end
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
I have an NPC that removes all money at once... I don't know if this should work for you, but I'll just show you an example.
Lua:
if(msgcontains(msg, 'training sword') or msgcontains(msg, 'gsc')) then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training sword}', cid)
                talkState[talkUser] = 1

        elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
            if(getPlayerItemCount(cid, 40) >= 2160) then
                doPlayerRemoveItem(cid, 40, 2160)
                doPlayerAddItem(cid, 2152, 1)
                doPlayerSendTextMessage(cid, 27,"you still have" .. getPlayerItemCount(cid,2160) .. " Crystal coin\'s.")
                selfSay('Thank you and come back often..', cid)
        else
                selfSay('You dont\s have 40k Crystal Coins.', cid)
        end
            talkState[talkUser] = 0
This serve can be used to exchange for items and other new currencies, any item you prefer.
 
Remove money count getPlayerItemCount & doPlayerRemoveItem
for ID's:
  • 2148 gold coins
  • 2152 platinum coins
  • 2160 crystal coins

And replace that with:
Lua:
doPlayerRemoveMoney(cid, money)

Example:
Lua:
if doPlayerRemoveMoney(cid, 400000) then
    local reward = doPlayerAddItem(cid, 5758, 1)
    doItemSetAttribute(reward, "charges", 100)
    selfSay(done, cid)
else
    selfSay(item, cid)
end
 
I have an NPC that removes all money at once... I don't know if this should work for you, but I'll just show you an example.
Lua:
if(msgcontains(msg, 'training sword') or msgcontains(msg, 'gsc')) then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training sword}', cid)
                talkState[talkUser] = 1

        elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
            if(getPlayerItemCount(cid, 40) >= 2160) then
                doPlayerRemoveItem(cid, 40, 2160)
                doPlayerAddItem(cid, 2152, 1)
                doPlayerSendTextMessage(cid, 27,"you still have" .. getPlayerItemCount(cid,2160) .. " Crystal coin\'s.")
                selfSay('Thank you and come back often..', cid)
        else
                selfSay('You dont\s have 40k Crystal Coins.', cid)
        end
            talkState[talkUser] = 0
This serve can be used to exchange for items and other new currencies, any item you prefer.
It's not quite what I'm looking for. If you look at the original script, it doesn't use the money grab function itself (for example if I had all the money in gold coin it would say I don't have any money). I would like to change this NPC's payment method to the correct function, I believe it is "doPlayerRemoveMoney".
 
Remove money count getPlayerItemCount & doPlayerRemoveItem
for ID's:
  • 2148 gold coins
  • 2152 platinum coins
  • 2160 crystal coins

And replace that with:
Lua:
doPlayerRemoveMoney(cid, money)

Example:
Lua:
if doPlayerRemoveMoney(cid, 400000) then
    local reward = doPlayerAddItem(cid, 5758, 1)
    doItemSetAttribute(reward, "charges", 100)
    selfSay(done, cid)
else
    selfSay(item, cid)
end
tried to put this way but got an error:
Lua:
        if msgcontains(msg, 'offer') then
        selfSay('Here you can buy the following training items: {training sword}, {training axe}, {training club}, {training bow}, {training rod}, and {training wand}.', cid)

            elseif msgcontains(msg, 'training sword') or msgcontains(msg, 'sword') then
                selfSay('Are you willing to pay 4,000,000 gold coins for {training sword}?', cid)
                talk_state = 1
            end

            elseif msgcontains(msg, 'yes') and talk_state == 1 then
            talk_state = 0
if doPlayerRemoveMoney(cid, 400000) then
    local reward = doPlayerAddItem(cid, 5758, 1)
    doItemSetAttribute(reward, "charges", 100)
    selfSay(done, cid)
else
    selfSay(item, cid)
end
2023-08-15 13:06:25 - [Error - NpcScript Interface]
2023-08-15 13:06:25 - (Unknown script file)
2023-08-15 13:06:25 - Description:
2023-08-15 13:06:25 - attempt to call a nil value
2023-08-15 13:06:25 - stack traceback:
 
Lua:
if msgcontains(msg, 'offer') then
    selfSay('Here you can buy the following training items: {training sword}, {training axe}, {training club}, {training bow}, {training rod}, and {training wand}.', cid)
elseif msgcontains(msg, 'training sword') or msgcontains(msg, 'sword') then
    selfSay('Are you willing to pay 4,000,000 gold coins for {training sword}?', cid)
    talk_state = 1
elseif msgcontains(msg, 'yes') and talk_state == 1 then
    talk_state = 0
    if doPlayerRemoveMoney(cid, 400000) then
        local reward = doPlayerAddItem(cid, 5758, 1)
        doItemSetAttribute(reward, "charges", 100)
        selfSay(done, cid)
    else
        selfSay(item, cid)
    end
end
 
Lua:
if msgcontains(msg, 'offer') then
    selfSay('Here you can buy the following training items: {training sword}, {training axe}, {training club}, {training bow}, {training rod}, and {training wand}.', cid)
elseif msgcontains(msg, 'training sword') or msgcontains(msg, 'sword') then
    selfSay('Are you willing to pay 4,000,000 gold coins for {training sword}?', cid)
    talk_state = 1
elseif msgcontains(msg, 'yes') and talk_state == 1 then
    talk_state = 0
    if doPlayerRemoveMoney(cid, 400000) then
        local reward = doPlayerAddItem(cid, 5758, 1)
        doItemSetAttribute(reward, "charges", 100)
        selfSay(done, cid)
    else
        selfSay(item, cid)
    end
end
Same error:
2023-08-15 13:41:54 - [Error - NpcScript Interface]
2023-08-15 13:41:54 - (Unknown script file)
2023-08-15 13:41:54 - Description:
2023-08-15 13:41:54 - attempt to call a nil value
2023-08-15 13:41:54 - stack traceback:
2023-08-15 13:41:54 -
2023-08-15 13:41:54 - [Error - NpcScript Interface]
2023-08-15 13:41:54 - (Unknown script file)
2023-08-15 13:41:54 - Description:
2023-08-15 13:41:54 - attempt to call a nil value
2023-08-15 13:41:54 - stack traceback:
 
Same error:
2023-08-15 13:41:54 - [Error - NpcScript Interface]
2023-08-15 13:41:54 - (Unknown script file)
2023-08-15 13:41:54 - Description:
2023-08-15 13:41:54 - attempt to call a nil value
2023-08-15 13:41:54 - stack traceback:
2023-08-15 13:41:54 -
2023-08-15 13:41:54 - [Error - NpcScript Interface]
2023-08-15 13:41:54 - (Unknown script file)
2023-08-15 13:41:54 - Description:
2023-08-15 13:41:54 - attempt to call a nil value
2023-08-15 13:41:54 - stack traceback:
I had to change something. I just tested it here and it worked. So make the script ther

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 item = 'You do not have the required items.'
local done = 'Here you are.'

local function handleTrainingPurchase(cid, cost, itemId)
    if doPlayerRemoveMoney(cid, cost) then
        selfSay('Are you willing to pay ' .. cost .. ' gold coins for this item?', cid)
        talk_state = itemId
    else
        selfSay('I need ' .. cost .. ' gold coins to give you this item. Come back when you have them.', cid)
        talk_state = 0
    end
end

local function confirmPurchase(cid, cost, itemId)
    if doPlayerRemoveMoney(cid, cost) then
        selfSay(done, cid)
        local newItem = doPlayerAddItem(cid, itemId, 1)
        doItemSetAttribute(newItem, "charges", 100)
    else
        selfSay(item, cid)
    end
    talk_state = 0
end

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

    if msgcontains(msg, 'offer') then
        selfSay('Here you can buy the following training items: {training sword}, {training axe}, {training club}, {training bow}, {training rod}, and {training wand}.', cid)
    elseif msgcontains(msg, 'training sword') or msgcontains(msg, 'sword') then
        handleTrainingPurchase(cid, 400000, 5758)
    elseif msgcontains(msg, 'training axe') or msgcontains(msg, 'axe') then
        handleTrainingPurchase(cid, 400000, 5759)
    elseif msgcontains(msg, 'training club') or msgcontains(msg, 'club') then
        handleTrainingPurchase(cid, 400000, 5760)
    elseif msgcontains(msg, 'training bow') or msgcontains(msg, 'bow') then
        handleTrainingPurchase(cid, 400000, 5761)
    elseif msgcontains(msg, 'training rod') or msgcontains(msg, 'rod') then
        handleTrainingPurchase(cid, 400000, 5756)
    elseif msgcontains(msg, 'training wand') or msgcontains(msg, 'wand') then
        handleTrainingPurchase(cid, 400000, 5757)
    elseif msgcontains(msg, 'yes') then
        if talk_state > 0 then
            confirmPurchase(cid, 400000, talk_state)
        end
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
@lursky helped me, thanks for atention and patience, follow 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 NO_ITEM_MSG = 'You do not have the required items.'
local SUCCESS_MSG = 'Here you are.'

reward_state = {}

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

    local money = getPlayerMoney(cid)
    if msgcontains(msg, 'offer') then
        selfSay('Here you can buy the following training items: {training sword}, {training axe}, {training club}, {training bow}, {training rod}, and {training wand}.', cid)
    elseif msgcontains(msg, 'training sword') or msgcontains(msg, 'sword') then
    local price = 4000000
    reward_state[cid] = { itemId = 5758, price = price }
    if money >= price then

            selfSay('Are you willing to pay 4,000,000 gold coins for {training sword}?', cid)
            talk_state = 1
        else
            selfSay('I need 4,000,000 gold coins to give you the {training sword}. Come back when you have them.', cid)
            talk_state = 0
        end
    -- axe ---
    elseif msgcontains(msg, 'training axe') or msgcontains(msg, 'axe') then
        local price = 4000000
        reward_state[cid] = { itemId = 5759, price = price }
        if money >= price then
            selfSay('Are you willing to pay 4,000,000 gold coins for {training axe}?', cid)
            talk_state = 1
        else
            selfSay('I need 4,000,000 gold coins to give you the {training axe}. Come back when you have them.', cid)
            talk_state = 0
        end
    -- club---
    elseif msgcontains(msg, 'training club') or msgcontains(msg, 'club') then
        local price = 4000000
        reward_state[cid] = { itemId = 5760, price = price }
        if money >= price then
            selfSay('Are you willing to pay 4,000,000 gold coins for {training club}?', cid)
            talk_state = 1
        else
            selfSay('I need 4,000,000 gold coins to give you the {training club}. Come back when you have them.', cid)
            talk_state = 0
        end
    -- pally--
    elseif msgcontains(msg, 'training bow') or msgcontains(msg, 'bow') then
        local price = 4000000
        reward_state[cid] = { itemId = 5761, price = price }
        if money >= price then
            selfSay('Are you willing to pay 4,000,000 gold coins for {training bow}?', cid)
            talk_state = 1
        else
            selfSay('I need 4,000,000 gold coins to give you the {training bow}. Come back when you have them.', cid)
            talk_state = 0
        end
    --druid--
    elseif msgcontains(msg, 'training rod') or msgcontains(msg, 'rod') then
        local price = 4000000
        reward_state[cid] = { itemId = 5756, price = price }
        if money >= price then
            selfSay('Are you willing to pay 4,000,000 gold coins for {training rod}?', cid)
            talk_state = 1
        else
            selfSay('I need 4,000,000 gold coins to give you the {training rod}. Come back when you have them.', cid)
            talk_state = 0
        end
    --- sorcerer--
    elseif msgcontains(msg, 'training wand') or msgcontains(msg, 'wand') then
        local price = 4000000
        reward_state[cid] = { itemId = 5757, price = price }
        if money >= price then
            selfSay('Are you willing to pay 4,000,000 gold coins for {training wand}?', cid)
            talk_state = 1
        else
            selfSay('I need 4,000,000 gold coins to give you the {training wand}. Come back when you have them.', cid)
            talk_state = 0
        end
    -- sell item logic
    elseif msgcontains(msg, 'yes') and talk_state == 1 then
        talk_state = 0
        local state = reward_state[cid]
        if not doPlayerRemoveMoney(cid, state.price) then
            selfSay(NO_ITEM_MSG, cid)
            return true
        end

        local charges = state.charges or 100
        local item = doPlayerAddItem(cid, state.itemId, state.count or 1)
        if item > 0 and charges > 1 then
            doItemSetAttribute(item, "charges", charges)
        end
        selfSay(SUCCESS_MSG, cid)
    end

    return true
end

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