itHyperoX Posted January 28, 2017 Share Posted January 28, 2017 (edited) function withdraw(player,cmd,number) local number = tonumber(number) local bankbalance = tonumber(getElementData(player, "account:bankbalance")) or 0 if not tonumber(number) then outputChatBox("#27B86C[USE]: #ffffff/" .. cmd .. " [Ammount]", player, 38, 194, 129, true) else if tonumber(bankbalance) <= 0 then outputChatBox("#B1092D[Error]: #ffffffYour bank balance is 0.", player, 177,9,45, true) elseif bankbalance >= tonumber(number) then givePlayerMoney(player,tonumber(number)) setElementData(player, "account:bankbalance", bankbalance - number) else outputChatBox("#B1092D[Error] #ffffffYou dont have enough money.", player, 177,9,45, true) end end end addCommandHandler("withdraw",withdraw) The problem is, when i depositing some money, like $500, and when i withdrawing like this: /withdraw -555555555555 the server give me thats money. But when the account balance is 0, i can't withdraw negative amount. How can i stop that, when player wihtdrawing -$500000000000 when the balance is in negavite ? I want thats, player ONLY can withdraw thats money, what they have on the account. Edited January 28, 2017 by TheMOG ww Link to comment
Mr.Loki Posted January 28, 2017 Share Posted January 28, 2017 You need to compare the amount with the amount in the bank and make sure that you are not withdrawing a greater value that what the bank has. function withdraw(player,cmd,number) local number = math.abs(tonumber(number)) local bankbalance = tonumber(getElementData(player, "account:bankbalance")) or 0 if not number then outputChatBox("#27B86C[USE]: #ffffff/" .. cmd .. " [Ammount]", player, 38, 194, 129, true) else if number > bankbalance then outputChatBox("#B1092D[Error]: #ffffffYour bank balance is "..bankbalance..".", player, 177,9,45, true) elseif bankbalance >= number then givePlayerMoney(player,number) setElementData(player, "account:bankbalance", bankbalance - number) --qoutputChatBox("#B1092D[Error] #ffffffYou dont have enough money.", player, 177,9,45, true) end end end addCommandHandler("withdraw",withdraw) This should work and no need to use tonumber so many times once is enough. Link to comment
itHyperoX Posted January 28, 2017 Author Share Posted January 28, 2017 (edited) ERROR: test/server.lua:43 bad argument #1 to 'abs' (number expected, got nil edit: i dont have problem with this. I have problem, when player withdrawing negative numbers. i talking about this: You have $5000 in your bank. when you type /withdraw 500 , then /withdraw -5555 the server give you the money. Edited January 28, 2017 by TheMOG d Link to comment
Addlibs Posted January 28, 2017 Share Posted January 28, 2017 (edited) This would mean that in the command, you've entered a value that couldn't get converted into a number (tonumber) and thus was converted to a nil value, which failed at math.abs. Edited January 28, 2017 by MrTasty Link to comment
idarrr Posted January 28, 2017 Share Posted January 28, 2017 function withdraw(player,cmd,number) if not number then -- This block should be in the first place before variable number defined. outputChatBox("#27B86C[USE]: #ffffff/" .. cmd .. " [Ammount]", player, 38, 194, 129, true) else local number = math.abs(tonumber(number)) local bankbalance = tonumber(getElementData(player, "account:bankbalance")) or 0 if number > bankbalance then outputChatBox("#B1092D[Error]: #ffffffYour bank balance is "..bankbalance..".", player, 177,9,45, true) elseif bankbalance >= number then givePlayerMoney(player,number) setElementData(player, "account:bankbalance", bankbalance - number) --qoutputChatBox("#B1092D[Error] #ffffffYou dont have enough money.", player, 177,9,45, true) end end end addCommandHandler("withdraw",withdraw) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now