Jump to content

Need a help with bank system!


itHyperoX

Recommended Posts

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 by TheMOG
ww
Link to comment

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

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 by TheMOG
d
Link to comment
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...