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