• 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+ Bank Withdraw Gold checks

CastorFlynn

Member
Joined
Aug 29, 2021
Messages
100
Reaction score
15
The bank script does not have a check for free slots in the player's container. When withdrawing amounts that occupy more slots, items are thrown to the floor.

Apparently there is no specific function in TFS for this, how could this check be done before handing over the gold to the player?

Nekiro Downgrade 1.3+

Bank.lua:
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local count = {}
local transfer = {}

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 rtrim(s)
  local n = #s
  while n > 0 and s:find("^%s", n) do n = n - 1 end
  return s:sub(1, n)
end

local function greetCallback(cid)
    count[cid], transfer[cid] = nil, nil
    return true
end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
---------------------------- balance ------------------------
    if msgcontains(msg, 'balance') then
        npcHandler.topic[cid] = 0
        if player:getBankBalance() >= 100000000 then
            npcHandler:say('I think you must be one of the richest inhabitants in the world! Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        elseif player:getBankBalance() >= 10000000 then
            npcHandler:say('You have made ten millions and it still grows! Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        elseif player:getBankBalance() >= 1000000 then
            npcHandler:say('Wow, you have reached the magic number of a million gp!!! Your account balance is ' .. player:getBankBalance() .. ' gold!', cid)
            return true
        elseif player:getBankBalance() >= 100000 then
            npcHandler:say('You certainly have made a pretty penny. Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        else
            npcHandler:say('Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        end
---------------------------- deposit ---------------------
    elseif msgcontains(msg, 'deposit') then
        count[cid] = player:getMoney()
        if count[cid] < 1 then
            npcHandler:say('You do not have enough gold.', cid)
            npcHandler.topic[cid] = 0
            return false
        end
        if msgcontains(msg, 'all') then
            count[cid] = player:getMoney()
            npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            if string.match(msg,'%d+') then
                count[cid] = getMoneyCount(msg)
                if count[cid] < 1 then
                    npcHandler:say('You do not have enough gold.', cid)
                    npcHandler.topic[cid] = 0
                    return false
                end
                npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
                npcHandler.topic[cid] = 2
                return true
            else
                npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
                npcHandler.topic[cid] = 1
                return true
            end
        end
        if not isValidMoney(count[cid]) then
            npcHandler:say('Sorry, but you can\'t deposit that much.', cid)
            npcHandler.topic[cid] = 0
            return false
        end
    elseif npcHandler.topic[cid] == 1 then
        count[cid] = getMoneyCount(msg)
        if isValidMoney(count[cid]) then
            npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            npcHandler:say('You do not have enough gold.', cid)
            npcHandler.topic[cid] = 0
            return true
        end
    elseif npcHandler.topic[cid] == 2 then
        if msgcontains(msg, 'yes') then
            if player:depositMoney(count[cid]) then
                npcHandler:say('Alright, we have added the amount of ' .. count[cid] .. ' gold to your {balance}. You can {withdraw} your money anytime you want to.', cid)
            else
                npcHandler:say('You do not have enough gold.', cid)
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('As you wish. Is there something else I can do for you?', cid)
        end
        npcHandler.topic[cid] = 0
        return true
---------------------------- withdraw --------------------
    elseif msgcontains(msg, 'withdraw') then
        if string.match(msg,'%d+') then
            count[cid] = getMoneyCount(msg)
            if isValidMoney(count[cid]) then
                npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
                npcHandler.topic[cid] = 7
            else
                npcHandler:say('There is not enough gold on your account.', cid)
                npcHandler.topic[cid] = 0
            end
            return true
        else
            npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
            npcHandler.topic[cid] = 6
            return true
        end
    elseif npcHandler.topic[cid] == 6 then
        count[cid] = getMoneyCount(msg)
        if isValidMoney(count[cid]) then
            npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
            npcHandler.topic[cid] = 7
        else
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
        end
        return true
    elseif npcHandler.topic[cid] == 7 then
        if msgcontains(msg, 'yes') then
            if player:getFreeCapacity() >= getMoneyWeight(count[cid]) then
                if not player:withdrawMoney(count[cid]) then
                    npcHandler:say('There is not enough gold on your account.', cid)
                else
                    npcHandler:say('Here you are, ' .. count[cid] .. ' gold. Please let me know if there is something else I can do for you.', cid)
                end
            else
                npcHandler:say('Whoah, hold on, you have no room in your inventory to carry all those coins. I don\'t want you to drop it on the floor, maybe come back with a cart!', cid)
            end
            npcHandler.topic[cid] = 0
        elseif msgcontains(msg, 'no') then
            npcHandler:say('The customer is king! Come back anytime you want to if you wish to {withdraw} your money.', cid)
            npcHandler.topic[cid] = 0
        end
        return true
---------------------------- transfer --------------------
    elseif msgcontains(msg, 'transfer') then
        npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
        npcHandler.topic[cid] = 11
    elseif npcHandler.topic[cid] == 11 then
        count[cid] = getMoneyCount(msg)
        if player:getBankBalance() < count[cid] then
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
            return true
        end
        if isValidMoney(count[cid]) then
            npcHandler:say('Who would you like transfer ' .. count[cid] .. ' gold to?', cid)
            npcHandler.topic[cid] = 12
        else
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
        end
    elseif npcHandler.topic[cid] == 12 then
        transfer[cid] = rtrim(msg)
        if player:getName() == transfer[cid] then
            npcHandler:say('Fill in this field with person who receives your gold!', cid)
            npcHandler.topic[cid] = 0
            return true
        end
        if playerExists(transfer[cid]) then
        local arrayDenied = {"accountmanager", "rooksample", "druidsample", "sorcerersample", "knightsample", "paladinsample"}
            if isInArray(arrayDenied, string.gsub(transfer[cid]:lower(), " ", "")) then
                npcHandler:say('This player does not exist.', cid)
                npcHandler.topic[cid] = 0
                return true
            end
            npcHandler:say('So you would like to transfer ' .. count[cid] .. ' gold to ' .. transfer[cid] .. '?', cid)
            npcHandler.topic[cid] = 13
        else
            npcHandler:say('This player does not exist.', cid)
            npcHandler.topic[cid] = 0
        end
    elseif npcHandler.topic[cid] == 13 then
        if msgcontains(msg, 'yes') then
            if not player:transferMoneyTo(transfer[cid], count[cid]) then
                npcHandler:say('You cannot transfer money to this account.', cid)
            else
                npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. transfer[cid] ..'.', cid)
                transfer[cid] = nil
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('Alright, is there something else I can do for you?', cid)
        end
        npcHandler.topic[cid] = 0
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, "Yes? What may I do for you, |PLAYERNAME|? Bank business, perhaps?")
npcHandler:setMessage(MESSAGE_FAREWELL, "Have a nice day.")
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
This bank NPC checks if the player has an empty slot in their backpack before giving them coins. If the player's backpack is full, the coins will not be delivered.

LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local count = {}
local transfer = {}

local function canReceiveMoney(player, amount)
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack then
        return false, "You don't have a backpack equipped."
    end

    local slots = backpack:getEmptySlots(true)
    if slots <= 0 then
        return false, "Your backpack is full. You need empty slots to receive money."
    end

    local weight = 0
    
    -- Crystal Coins (10000 gold each)
    local crystals = math.floor(amount / 10000)
    amount = amount - (crystals * 10000)
    weight = weight + (crystals * ItemType(2160):getWeight())

    -- Platinum Coins (100 gold each)
    local platinum = math.floor(amount / 100)
    amount = amount - (platinum * 100)
    weight = weight + (platinum * ItemType(2152):getWeight())

    -- Gold Coins
    weight = weight + (amount * ItemType(2148):getWeight())

    if player:getFreeCapacity() < weight then
        return false, "The money would be too heavy for you to carry."
    end

    return true, ""
end

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 rtrim(s)
    local n = #s
    while n > 0 and s:find("^%s", n) do n = n - 1 end
    return s:sub(1, n)
end

local function greetCallback(cid)
    count[cid], transfer[cid] = nil, nil
    return true
end

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

---------------------------- balance ------------------------
    if msgcontains(msg, 'balance') then
        npcHandler.topic[cid] = 0
        if player:getBankBalance() >= 100000000 then
            npcHandler:say('I think you must be one of the richest inhabitants in the world! Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        elseif player:getBankBalance() >= 10000000 then
            npcHandler:say('You have made ten millions and it still grows! Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        elseif player:getBankBalance() >= 1000000 then
            npcHandler:say('Wow, you have reached the magic number of a million gp!!! Your account balance is ' .. player:getBankBalance() .. ' gold!', cid)
            return true
        elseif player:getBankBalance() >= 100000 then
            npcHandler:say('You certainly have made a pretty penny. Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        else
            npcHandler:say('Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        end

---------------------------- deposit ------------------------
    elseif msgcontains(msg, 'deposit') then
        count[cid] = player:getMoney()
        if count[cid] < 1 then
            npcHandler:say('You do not have enough gold.', cid)
            npcHandler.topic[cid] = 0
            return false
        end
        if msgcontains(msg, 'all') then
            count[cid] = player:getMoney()
            npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            if string.match(msg,'%d+') then
                count[cid] = getMoneyCount(msg)
                if count[cid] < 1 then
                    npcHandler:say('You do not have enough gold.', cid)
                    npcHandler.topic[cid] = 0
                    return false
                end
                npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
                npcHandler.topic[cid] = 2
                return true
            else
                npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
                npcHandler.topic[cid] = 1
                return true
            end
        end
    elseif npcHandler.topic[cid] == 1 then
        count[cid] = getMoneyCount(msg)
        if isValidMoney(count[cid]) then
            npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            npcHandler:say('You do not have enough gold.', cid)
            npcHandler.topic[cid] = 0
            return true
        end
    elseif npcHandler.topic[cid] == 2 then
        if msgcontains(msg, 'yes') then
            if player:depositMoney(count[cid]) then
                npcHandler:say('Alright, we have added the amount of ' .. count[cid] .. ' gold to your {balance}. You can {withdraw} your money anytime you want to.', cid)
            else
                npcHandler:say('You do not have enough gold.', cid)
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('As you wish. Is there something else I can do for you?', cid)
        end
        npcHandler.topic[cid] = 0
        return true

---------------------------- withdraw ------------------------
    elseif msgcontains(msg, 'withdraw') then
        if string.match(msg,'%d+') then
            count[cid] = getMoneyCount(msg)
            if isValidMoney(count[cid]) then
                npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
                npcHandler.topic[cid] = 7
            else
                npcHandler:say('There is not enough gold on your account.', cid)
                npcHandler.topic[cid] = 0
            end
            return true
        else
            npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
            npcHandler.topic[cid] = 6
            return true
        end
    elseif npcHandler.topic[cid] == 6 then
        count[cid] = getMoneyCount(msg)
        if isValidMoney(count[cid]) then
            npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
            npcHandler.topic[cid] = 7
        else
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
        end
        return true
    elseif npcHandler.topic[cid] == 7 then
        if msgcontains(msg, 'yes') then
            local canReceive, errorMsg = canReceiveMoney(player, count[cid])
            if not canReceive then
                npcHandler:say(errorMsg, cid)
                npcHandler.topic[cid] = 0
                return true
            end

            if not player:withdrawMoney(count[cid]) then
                npcHandler:say('There is not enough gold on your account.', cid)
            else
                npcHandler:say('Here you are, ' .. count[cid] .. ' gold. Please let me know if there is something else I can do for you.', cid)
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('The customer is king! Come back anytime you want to if you wish to {withdraw} your money.', cid)
        end
        npcHandler.topic[cid] = 0
        return true

---------------------------- transfer ------------------------
    elseif msgcontains(msg, 'transfer') then
        npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
        npcHandler.topic[cid] = 11
    elseif npcHandler.topic[cid] == 11 then
        count[cid] = getMoneyCount(msg)
        if player:getBankBalance() < count[cid] then
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
            return true
        end
        if isValidMoney(count[cid]) then
            npcHandler:say('Who would you like transfer ' .. count[cid] .. ' gold to?', cid)
            npcHandler.topic[cid] = 12
        else
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
        end
    elseif npcHandler.topic[cid] == 12 then
        transfer[cid] = rtrim(msg)
        if player:getName() == transfer[cid] then
            npcHandler:say('Fill in this field with person who receives your gold!', cid)
            npcHandler.topic[cid] = 0
            return true
        end
        if playerExists(transfer[cid]) then
            local arrayDenied = {"accountmanager", "rooksample", "druidsample", "sorcerersample", "knightsample", "paladinsample"}
            if isInArray(arrayDenied, string.gsub(transfer[cid]:lower(), " ", "")) then
                npcHandler:say('This player does not exist.', cid)
                npcHandler.topic[cid] = 0
                return true
            end
            npcHandler:say('So you would like to transfer ' .. count[cid] .. ' gold to ' .. transfer[cid] .. '?', cid)
            npcHandler.topic[cid] = 13
        else
            npcHandler:say('This player does not exist.', cid)
            npcHandler.topic[cid] = 0
        end
    elseif npcHandler.topic[cid] == 13 then
        if msgcontains(msg, 'yes') then
            if not player:transferMoneyTo(transfer[cid], count[cid]) then
                npcHandler:say('You cannot transfer money to this account.', cid)
            else
                npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. transfer[cid] ..'.', cid)
                transfer[cid] = nil
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('Alright, is there something else I can do for you?', cid)
        end
        npcHandler.topic[cid] = 0
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, "Yes? What may I do for you, |PLAYERNAME|? Bank business, perhaps?")
npcHandler:setMessage(MESSAGE_FAREWELL, "Have a nice day.")
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
This bank NPC checks if the player has an empty slot in their backpack before giving them coins. If the player's backpack is full, the coins will not be delivered.

LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local count = {}
local transfer = {}

local function canReceiveMoney(player, amount)
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack then
        return false, "You don't have a backpack equipped."
    end

    local slots = backpack:getEmptySlots(true)
    if slots <= 0 then
        return false, "Your backpack is full. You need empty slots to receive money."
    end

    local weight = 0
   
    -- Crystal Coins (10000 gold each)
    local crystals = math.floor(amount / 10000)
    amount = amount - (crystals * 10000)
    weight = weight + (crystals * ItemType(2160):getWeight())

    -- Platinum Coins (100 gold each)
    local platinum = math.floor(amount / 100)
    amount = amount - (platinum * 100)
    weight = weight + (platinum * ItemType(2152):getWeight())

    -- Gold Coins
    weight = weight + (amount * ItemType(2148):getWeight())

    if player:getFreeCapacity() < weight then
        return false, "The money would be too heavy for you to carry."
    end

    return true, ""
end

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 rtrim(s)
    local n = #s
    while n > 0 and s:find("^%s", n) do n = n - 1 end
    return s:sub(1, n)
end

local function greetCallback(cid)
    count[cid], transfer[cid] = nil, nil
    return true
end

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

---------------------------- balance ------------------------
    if msgcontains(msg, 'balance') then
        npcHandler.topic[cid] = 0
        if player:getBankBalance() >= 100000000 then
            npcHandler:say('I think you must be one of the richest inhabitants in the world! Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        elseif player:getBankBalance() >= 10000000 then
            npcHandler:say('You have made ten millions and it still grows! Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        elseif player:getBankBalance() >= 1000000 then
            npcHandler:say('Wow, you have reached the magic number of a million gp!!! Your account balance is ' .. player:getBankBalance() .. ' gold!', cid)
            return true
        elseif player:getBankBalance() >= 100000 then
            npcHandler:say('You certainly have made a pretty penny. Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        else
            npcHandler:say('Your account balance is ' .. player:getBankBalance() .. ' gold.', cid)
            return true
        end

---------------------------- deposit ------------------------
    elseif msgcontains(msg, 'deposit') then
        count[cid] = player:getMoney()
        if count[cid] < 1 then
            npcHandler:say('You do not have enough gold.', cid)
            npcHandler.topic[cid] = 0
            return false
        end
        if msgcontains(msg, 'all') then
            count[cid] = player:getMoney()
            npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            if string.match(msg,'%d+') then
                count[cid] = getMoneyCount(msg)
                if count[cid] < 1 then
                    npcHandler:say('You do not have enough gold.', cid)
                    npcHandler.topic[cid] = 0
                    return false
                end
                npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
                npcHandler.topic[cid] = 2
                return true
            else
                npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
                npcHandler.topic[cid] = 1
                return true
            end
        end
    elseif npcHandler.topic[cid] == 1 then
        count[cid] = getMoneyCount(msg)
        if isValidMoney(count[cid]) then
            npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            npcHandler:say('You do not have enough gold.', cid)
            npcHandler.topic[cid] = 0
            return true
        end
    elseif npcHandler.topic[cid] == 2 then
        if msgcontains(msg, 'yes') then
            if player:depositMoney(count[cid]) then
                npcHandler:say('Alright, we have added the amount of ' .. count[cid] .. ' gold to your {balance}. You can {withdraw} your money anytime you want to.', cid)
            else
                npcHandler:say('You do not have enough gold.', cid)
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('As you wish. Is there something else I can do for you?', cid)
        end
        npcHandler.topic[cid] = 0
        return true

---------------------------- withdraw ------------------------
    elseif msgcontains(msg, 'withdraw') then
        if string.match(msg,'%d+') then
            count[cid] = getMoneyCount(msg)
            if isValidMoney(count[cid]) then
                npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
                npcHandler.topic[cid] = 7
            else
                npcHandler:say('There is not enough gold on your account.', cid)
                npcHandler.topic[cid] = 0
            end
            return true
        else
            npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
            npcHandler.topic[cid] = 6
            return true
        end
    elseif npcHandler.topic[cid] == 6 then
        count[cid] = getMoneyCount(msg)
        if isValidMoney(count[cid]) then
            npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
            npcHandler.topic[cid] = 7
        else
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
        end
        return true
    elseif npcHandler.topic[cid] == 7 then
        if msgcontains(msg, 'yes') then
            local canReceive, errorMsg = canReceiveMoney(player, count[cid])
            if not canReceive then
                npcHandler:say(errorMsg, cid)
                npcHandler.topic[cid] = 0
                return true
            end

            if not player:withdrawMoney(count[cid]) then
                npcHandler:say('There is not enough gold on your account.', cid)
            else
                npcHandler:say('Here you are, ' .. count[cid] .. ' gold. Please let me know if there is something else I can do for you.', cid)
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('The customer is king! Come back anytime you want to if you wish to {withdraw} your money.', cid)
        end
        npcHandler.topic[cid] = 0
        return true

---------------------------- transfer ------------------------
    elseif msgcontains(msg, 'transfer') then
        npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
        npcHandler.topic[cid] = 11
    elseif npcHandler.topic[cid] == 11 then
        count[cid] = getMoneyCount(msg)
        if player:getBankBalance() < count[cid] then
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
            return true
        end
        if isValidMoney(count[cid]) then
            npcHandler:say('Who would you like transfer ' .. count[cid] .. ' gold to?', cid)
            npcHandler.topic[cid] = 12
        else
            npcHandler:say('There is not enough gold on your account.', cid)
            npcHandler.topic[cid] = 0
        end
    elseif npcHandler.topic[cid] == 12 then
        transfer[cid] = rtrim(msg)
        if player:getName() == transfer[cid] then
            npcHandler:say('Fill in this field with person who receives your gold!', cid)
            npcHandler.topic[cid] = 0
            return true
        end
        if playerExists(transfer[cid]) then
            local arrayDenied = {"accountmanager", "rooksample", "druidsample", "sorcerersample", "knightsample", "paladinsample"}
            if isInArray(arrayDenied, string.gsub(transfer[cid]:lower(), " ", "")) then
                npcHandler:say('This player does not exist.', cid)
                npcHandler.topic[cid] = 0
                return true
            end
            npcHandler:say('So you would like to transfer ' .. count[cid] .. ' gold to ' .. transfer[cid] .. '?', cid)
            npcHandler.topic[cid] = 13
        else
            npcHandler:say('This player does not exist.', cid)
            npcHandler.topic[cid] = 0
        end
    elseif npcHandler.topic[cid] == 13 then
        if msgcontains(msg, 'yes') then
            if not player:transferMoneyTo(transfer[cid], count[cid]) then
                npcHandler:say('You cannot transfer money to this account.', cid)
            else
                npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. transfer[cid] ..'.', cid)
                transfer[cid] = nil
            end
        elseif msgcontains(msg, 'no') then
            npcHandler:say('Alright, is there something else I can do for you?', cid)
        end
        npcHandler.topic[cid] = 0
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, "Yes? What may I do for you, |PLAYERNAME|? Bank business, perhaps?")
npcHandler:setMessage(MESSAGE_FAREWELL, "Have a nice day.")
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

Unfortunately it didn't work. The free slots were filled, but the rest went to the ground.
 
I didn't check the rest of the code, just updated /changed the canReceiveMoney function.

amount 59 = 1 slot required
amount 22222 = 3 slots required
amount 1234567 = 4 slots required

LUA:
local function canReceiveMoney(player, amount)
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack then
        return false, "You don't have a backpack equipped."
    end
  
    -- Calculate total weight
    local crystal = math.floor(amount / 10000)
    local remainingAfterCrystal = amount % 10000
  
    local platinum = math.floor(remainingAfterCrystal / 100)
    local remainingAfterPlatinum = remainingAfterCrystal % 100
  
    local gold = remainingAfterPlatinum -- not required but added to make it more clear
  
    local crystalWeight = crystal * ItemType(2160):getWeight()
    local platinumWeight = platinum * ItemType(2152):getWeight()
    local goldWeight = gold * ItemType(2148):getWeight()
  
    local totalMoneyWeight = crystalWeight + platinumWeight + goldWeight
  
    if player:getFreeCapacity() < totalMoneyWeight then
        return false, "The money would be too heavy for you to carry."
    end
  
    -- Calculate total slots required
    local crystalSlots = math.ceil(crystal / 100)
    local platinumSlots = math.ceil(platinum / 100)
    local goldSlots = math.ceil(gold / 100)
  
    local totalSlotsRequired = crystalSlots + platinumSlots + goldSlots
    local availableSlots = backpack:getEmptySlots(true)
  
    if totalSlotsRequired > availableSlots then
        return false, "Your backpack doesn't have enough empty slots to receive all the money."
    end
  
    return true, ""
end
 
I didn't check the rest of the code, just updated /changed the canReceiveMoney function.

amount 59 = 1 slot required
amount 22222 = 3 slots required
amount 1234567 = 4 slots required

LUA:
local function canReceiveMoney(player, amount)
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack then
        return false, "You don't have a backpack equipped."
    end
 
    -- Calculate total weight
    local crystal = math.floor(amount / 10000)
    local remainingAfterCrystal = amount % 10000
 
    local platinum = math.floor(remainingAfterCrystal / 100)
    local remainingAfterPlatinum = remainingAfterCrystal % 100
 
    local gold = remainingAfterPlatinum -- not required but added to make it more clear
 
    local crystalWeight = crystal * ItemType(2160):getWeight()
    local platinumWeight = platinum * ItemType(2152):getWeight()
    local goldWeight = gold * ItemType(2148):getWeight()
 
    local totalMoneyWeight = crystalWeight + platinumWeight + goldWeight
 
    if player:getFreeCapacity() < totalMoneyWeight then
        return false, "The money would be too heavy for you to carry."
    end
 
    -- Calculate total slots required
    local crystalSlots = math.ceil(crystal / 100)
    local platinumSlots = math.ceil(platinum / 100)
    local goldSlots = math.ceil(gold / 100)
 
    local totalSlotsRequired = crystalSlots + platinumSlots + goldSlots
    local availableSlots = backpack:getEmptySlots(true)
 
    if totalSlotsRequired > availableSlots then
        return false, "Your backpack doesn't have enough empty slots to receive all the money."
    end
 
    return true, ""
end
It worked! Thank you!
 

Similar threads

Back
Top