• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

TFS 1.X+ Cant get working multi word name support on bank

Tbol

Well-Known Member
Joined
Apr 7, 2019
Messages
625
Reaction score
71
Cant get this code support players that contains two or more words in their name it works fine if their name contains one word, because i think this means that if the players name contains spaces ( "Cloudy Test"), only "Cloudy" will be recognized as the player name, and "Test" will be treated as part of the amount, thats why i always get Proper format is "name amount"', but it works perfect if i do 'Name 2000' because it has one word in his name so its being treated correctly, tried fixing it for 2 hours and couldnt think anything

LUA:
function Player:transferMoneyTo(targetName, amount)
    local targetPlayerId = getPlayerByName(targetName)
    if not targetPlayerId then
        return false, "Target player not found or invalid"
    end

    local targetPlayer = Player(targetPlayerId)
    if not targetPlayer then
        return false, "Target player object could not be retrieved"
    end

    if self:getBankBalance() < amount then
        return false, "Insufficient funds"
    end

    self:setBankBalance(self:getBankBalance() - amount)
    targetPlayer:setBankBalance(targetPlayer:getBankBalance() + amount)
    return true, "Transfer successful"
end
LUA:
    elseif action == "transfer" then
        local target = data['name']
        local amount = data['amount']
        if amount == "all" then
            amount = player:getBankBalance()
        end

        if not target or not amount then
            sendJSON2(player, "sendError", { message = 'Proper format is "name amount"' }, OPCODE_BANK_INFO)
            return false
        end

        amount = tonumber(amount)
        if not amount or amount < 1 then
            sendJSON2(player, "sendError", { message = 'Invalid transfer amount' }, OPCODE_BANK_INFO)
            return false
        end

        if target:lower() == player:getName():lower() or string.len(target) < 3 then
            sendJSON2(player, "sendError", { message = 'Invalid target name' }, OPCODE_BANK_INFO)
            return false
        end

        if player:getBankBalance() < amount then
            sendJSON2(player, "sendError", { message = "Insufficient bank balance" }, OPCODE_BANK_INFO)
            return false
        end

        local success, message = player:transferMoneyTo(target, amount)
        if not success then
            sendJSON2(player, "sendError", { message = message }, OPCODE_BANK_INFO)
            return false
        end

        sendJSON2(player, "sendBalance", { balance = player:getBankBalance() }, OPCODE_BANK_INFO)
    else
        sendJSON2(player, "sendError", { message = "Unknown action" }, OPCODE_BANK_INFO)
    end
 
parseTransferInput parses the input to separate the target name and the amount. This should handle cases where names contain spaces.

LUA:
function parseTransferInput(input)
    local amount = tonumber(input:match("%d+$"))
    if not amount then
        return nil, nil, "Invalid amount"
    end
    
    local target = input:sub(1, input:len() - tostring(amount):len()):match("^%s*(.-)%s*$")
    if not target or target == "" then
        return nil, nil, "Invalid target name"
    end

    return target, amount, nil
end

elseif action == "transfer" then
    local target, amount, err = parseTransferInput(data['name'])
    if err then
        sendJSON2(player, "sendError", { message = err }, OPCODE_BANK_INFO)
        return false
    end

    if amount == "all" then
        amount = player:getBankBalance()
    end

    if player:getBankBalance() < amount then
        sendJSON2(player, "sendError", { message = "Insufficient bank balance" }, OPCODE_BANK_INFO)
        return false
    end

    local success, message = player:transferMoneyTo(target, amount)
    if not success then
        sendJSON2(player, "sendError", { message = message }, OPCODE_BANK_INFO)
        return false
    end

    sendJSON2(player, "sendBalance", { balance = player:getBankBalance() }, OPCODE_BANK_INFO)
else
    sendJSON2(player, "sendError", { message = "Unknown action" }, OPCODE_BANK_INFO)
end
 
parseTransferInput parses the input to separate the target name and the amount. This should handle cases where names contain spaces.

LUA:
function parseTransferInput(input)
    local amount = tonumber(input:match("%d+$"))
    if not amount then
        return nil, nil, "Invalid amount"
    end
   
    local target = input:sub(1, input:len() - tostring(amount):len()):match("^%s*(.-)%s*$")
    if not target or target == "" then
        return nil, nil, "Invalid target name"
    end

    return target, amount, nil
end

elseif action == "transfer" then
    local target, amount, err = parseTransferInput(data['name'])
    if err then
        sendJSON2(player, "sendError", { message = err }, OPCODE_BANK_INFO)
        return false
    end

    if amount == "all" then
        amount = player:getBankBalance()
    end

    if player:getBankBalance() < amount then
        sendJSON2(player, "sendError", { message = "Insufficient bank balance" }, OPCODE_BANK_INFO)
        return false
    end

    local success, message = player:transferMoneyTo(target, amount)
    if not success then
        sendJSON2(player, "sendError", { message = message }, OPCODE_BANK_INFO)
        return false
    end

    sendJSON2(player, "sendBalance", { balance = player:getBankBalance() }, OPCODE_BANK_INFO)
else
    sendJSON2(player, "sendError", { message = "Unknown action" }, OPCODE_BANK_INFO)
end
Always says invalid amount
 
Back
Top