• 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!
  • If you're using Gesior 2012 or MyAAC, please review this thread for information about a serious security vulnerability and a fix.

TalkAction [TFS] Banking system with functional transfer to offline players

UberLerd

Member
Joined
Dec 24, 2017
Messages
25
Solutions
1
Reaction score
18
Hello everyone,

I found a wonderful banking system at stable 1.2 tfs bank system (https://otland.net/threads/stable-1-2-tfs-bank-system.241915/#post-2342429) however I thought the inability to transfer to offline players was disappointing, and also the withdraw and transfer functions didn't check for funds already bid toward a house, so I've modified the script to fix those.

I also wasn't a fan of the popup windows, so now it's just console messages. I hope this helps anyone looking for this solution!

Corrected Code:
Lua:
local function getPlayerBiddedMoney(player)
    local resultId = db.storeQuery('SELECT `bid` FROM `houses` WHERE `highest_bidder`=' .. db.escapeString(getPlayerGUID(player)) .. ' LIMIT 1')
    if resultId == false then
        return 0
    end
    local r = result.getDataInt(resultId, "bid")
    result.free(resultId)
    return r
end
local function findPlayer(name)
    local resultId = db.storeQuery('SELECT name FROM players WHERE name=' .. db.escapeString(name) .. ' LIMIT 1'), nil
    if resultId == false then
        return
    end
    local r = result.getDataString(resultId, "name")
    result.free(resultId)
    return r
end
function onSay(player, words, param)
    if words == "!deposit" then
        t = string.split(param, ",")
      
        amount = t[1]
        if amount ~= "all" then       
        amount = tonumber(t[1])       
        if amount > 0 then
            if player:removeMoney(amount) then
            local old_balance = getPlayerBalance(player)
                doPlayerSetBalance(player, old_balance + amount)
                --send deposit message to player
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have deposited " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
            else
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have this much gold.")
            return false
            end
        end
        else
            player_gold = player:getMoney()           
            if player_gold > 0 then
            local old_balance = getPlayerBalance(player)
                player:removeMoney(player_gold)
                doPlayerSetBalance(player, old_balance + player_gold)
                --send deposit message to player
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have deposited " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
            else
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have any gold to deposit.")
            return false
            end
        end
      
    elseif words == "!withdraw" then   
        t = string.split(param, ",") 
    amount = t[1]
    if amount ~= "all" then   
    amount = tonumber(t[1])
        if amount <= 0 then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You must type a valid value to withdraw.")
        return false
        end       
        if amount > 100000000 then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You cannot withdraw more then 100 million gold.")
        return false
        end       
        if getPlayerBalance(player) - getPlayerBiddedMoney(player) < amount then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have this much gold available for withdrawal.")
        return false
        end   
      
        if doPlayerSetBalance(player, getPlayerBalance(player) - amount) then
            player:addMoney(amount)
            --send withdraw message to player
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have withdrawn " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
        end
        else
        local balance = getPlayerBalance(player)
        if balance > 0 then
                local old_balance = getPlayerBalance(player)
            if doPlayerSetBalance(player, getPlayerBalance(player) - balance) then
                player:addMoney(balance)
                --send withdraw message to player 
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have withdrawn " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
            end   
        else
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have gold to withraw!")   
        end
    end      
    elseif words == "!balance" then
        --send balance message to player
        doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "Your account balance is " .. getPlayerBalance(player) .. " gold.")
      
    elseif words == "!transfer" then
  
        t = string.split(param, ",")
      
        target = Player(t[1])
        targetname = getPlayerName(t[1])
        offlinetargetname = t[1]
        amount = tonumber(t[2])
      
        if not t[1] or not t[2] then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You must type like: !transfer player, amount")
        return false
        end
      
        if amount <= 0 then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You must transfer 1 or more gold.")
        return false
        end
      
        if getPlayerBalance(player) - getPlayerBiddedMoney(player) < amount then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have this much gold available for transfer.")
        return false
        end
        --if target player for transfer is online
        if target then
            doPlayerSetBalance(player, getPlayerBalance(player) - amount)
            doPlayerSetBalance(target, getPlayerBalance(target) + amount)
            --send transfer message to player
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You transfered " .. amount .. " gold to " .. targetname .. ". Your account balance is " .. getPlayerBalance(player) .. " gold.")
            --send transfer receipt message to target player
            doPlayerSendTextMessage(target, MESSAGE_INFO_DESCR, "" .. getPlayerName(player) .. " transfered " ..amount.. " gold to your account.")
        --if target player for transfer is offline
        elseif findPlayer(offlinetargetname) ~= nil then
            doPlayerSetBalance(player, getPlayerBalance(player) - amount)
            db.query('UPDATE players SET balance=balance+' .. amount .. ' WHERE name=' .. db.escapeString(offlinetargetname) .. ' LIMIT 1')
            --send transfer message to player
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You transfered " .. amount .. " gold to " .. offlinetargetname .. ". Your account balance is " .. getPlayerBalance(player) .. " gold.")
        --if target player is not found
        else
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "The player " .. offlinetargetname .. " does not exist.")
            return false
        end
    end
    return false
end

Don't forget to call the new script in your talkactions.xml
XML:
    <talkaction words="!deposit" separator=" " script="banking.lua" />
    <talkaction words="!withdraw" separator=" " script="banking.lua" />
    <talkaction words="!balance" separator=" " script="banking.lua" />
    <talkaction words="!transfer" separator=" " script="banking.lua" />



Bad Code:
Lua:
local function getPlayerBiddedMoney(player)
    local resultId = db.storeQuery('SELECT `bid` FROM `houses` WHERE `highest_bidder`=' .. db.escapeString(getPlayerGUID(player)) .. ' LIMIT 1')
    if resultId == false then
        return 0
    end
    local r = result.getDataInt(resultId, "bid")
    result.free(resultId)
    return r
end

function onSay(player, words, param)
    if words == "!deposit" then
        t = string.split(param, ",")
    
        amount = t[1]
        if amount ~= "all" then     
        amount = tonumber(t[1])     
        if amount > 0 then
            if player:removeMoney(amount) then
            local old_balance = getPlayerBalance(player)
                doPlayerSetBalance(player, old_balance + amount)
                --send deposit message to player
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have deposited " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
            else
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have this much gold.")
            return false
            end
        end
        else
            player_gold = player:getMoney()         
            if player_gold > 0 then
            local old_balance = getPlayerBalance(player)
                player:removeMoney(player_gold)
                doPlayerSetBalance(player, old_balance + player_gold)
                --send deposit message to player
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have deposited " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
            else
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have any gold to deposit.")
            return false
            end
        end
    
    elseif words == "!withdraw" then 
        t = string.split(param, ",")
    amount = t[1]
    if amount ~= "all" then 
    amount = tonumber(t[1])
        if amount <= 0 then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You must type a valid value to withdraw.")
        return false
        end     
        if amount > 100000000 then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You cannot withdraw more then 100 million gold.")
        return false
        end     
        if getPlayerBalance(player) - getPlayerBiddedMoney(player) < amount then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have this much gold available for withdrawal.")
        return false
        end 
    
        if doPlayerSetBalance(player, getPlayerBalance(player) - amount) then
            player:addMoney(amount)
            --send withdraw message to player
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have withdrawn " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
        end
        else
        local balance = getPlayerBalance(player)
        if balance > 0 then
                local old_balance = getPlayerBalance(player)
            if doPlayerSetBalance(player, getPlayerBalance(player) - balance) then
                player:addMoney(balance)
                --send withdraw message to player
                doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You have withdrawn " .. amount .. " gold. Your account balance is now " .. getPlayerBalance(player) .. ".")
            end 
        else
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have gold to withraw!") 
        end
    end   
    elseif words == "!balance" then
        --send balance message to player
        doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "Your account balance is " .. getPlayerBalance(player) .. " gold.")
    
    elseif words == "!transfer" then
 
        t = string.split(param, ",")
    
        target = Player(t[1])
        targetname = getPlayerName(t[1])
        offlinetargetname = t[1]
        amount = tonumber(t[2])
    
        if not t[1] or not t[2] then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You must type like: !transfer player, amount")
        return false
        end
    
        if amount <= 0 then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You must transfer 1 or more gold.")
        return false
        end
    
        if getPlayerBalance(player) - getPlayerBiddedMoney(player) < amount then
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You do not have this much gold available for transfer.")
        return false
        end

        --if target player for transfer is online
        if target then
            doPlayerSetBalance(player, getPlayerBalance(player) - amount)
            doPlayerSetBalance(target, getPlayerBalance(target) + amount)
            --send transfer message to player
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You transfered "..amount.." gold to " .. targetname .. ". Your account balance is " .. getPlayerBalance(player) .. " gold.")
            --send transfer receipt message to target player
            doPlayerSendTextMessage(target, MESSAGE_INFO_DESCR, "" .. getPlayerName(player) .. " transfered "..amount.." gold to your account.")
        --if target player for transfer is offline
        elseif target == nil then
            doPlayerSetBalance(player, getPlayerBalance(player) - amount)
            db.query('UPDATE players SET balance=balance+' .. amount .. ' WHERE name=' .. db.escapeString(offlinetargetname) .. ' LIMIT 1')
            --send transfer message to player
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "You transfered "..amount.." gold to " .. offlinetargetname .. ". Your account balance is " .. getPlayerBalance(player) .. " gold.")
        --if target player is not found
        else
            doPlayerSendTextMessage(player, MESSAGE_INFO_DESCR, "This player does not exist.")
            return false
        end
    end
    return false
end
 
Last edited:

Boy67

Veteran OT User
Joined
Jul 4, 2007
Messages
468
Solutions
26
Reaction score
371
not bad.

A few things are wrong with your script just from having a quick glance.

If the target player is offline and doesn't exist, the player will lose still their money. The else statement on line 122 is also not needed.

Other than that, good job!
 
OP
OP
UberLerd

UberLerd

Member
Joined
Dec 24, 2017
Messages
25
Solutions
1
Reaction score
18
not bad.

A few things are wrong with your script just from having a quick glance.

If the target player is offline and doesn't exist, the player will lose still their money. The else statement on line 122 is also not needed.

Other than that, good job!
Thanks, I’ll get that fixed and edit OP. 🙏
 
Top