Jump to content

[HELP] Bank System


Imposter

Recommended Posts

Posted

i want it to check if the player has a value for bankMoney, if not then it sets it as 0 but i cant get it to work cause im a noob.

serverside

  
function checkBalance() 
    playerName = getPlayerName(thePlayer) 
    account = getPlayerAccount ( source ) 
    bankMoney = getAccountData ( account, "bankcash" ) 
    if ( bankMoney==nil ) then 
        setAccountData ( account, "bankCash", 0 ) 
        bankMoney=0 
    else 
        outputChatBox (  "EagleBot: " .. playerName .. " has $" .. bankMoney .. " in his bank!" ) 
    end 
end 
addCommandHandler ( "bank" , checkBalance ) 
  

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted
  
function checkBalance(thePlayer) 
    local account = getPlayerAccount ( thePlayer ) 
    local bankMoney = getAccountData ( account, "bankcash" ) 
    if ( bankMoney==false ) then -- getAccountData returns false not nil, so check if false 
        setAccountData ( account, "bankCash", 0 ) 
        bankMoney=0 
    else 
        outputChatBox (  "EagleBot: " .. getPlayerName(thePlayer) .. " has $" .. bankMoney .. " in his bank!" ) 
    end 
end 
addCommandHandler ( "bank" , checkBalance ) 
  

Posted

You don't have "thePlayer" defined anywhere.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
  
function checkBalance(thePlayer) 
    local account = getPlayerAccount ( thePlayer ) 
    local bankMoney = getAccountData ( account, "bankcash" ) 
    if ( bankMoney==false ) then -- getAccountData returns false not nil, so check if false 
        setAccountData ( account, "bankCash", 0 ) 
        bankMoney=0 
    else 
        outputChatBox (  "EagleBot: " .. getPlayerName(thePlayer) .. " has $" .. bankMoney .. " in his bank!" ) 
    end 
end 
addCommandHandler ( "bank" , checkBalance ) 
  

What the hell...?? this is a retarded error..im using npp

[2012-07-25 14:05:02] WARNING: Loading script failed: [roleplay]\bank\server.lua:2: unexpected symbol near 'Â'

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted

Must be the Encoding.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
I don't know try to write this line on your own.

yea, i figured that is what happened, its all good now, thanks again AMARANT and SolidSnake14

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted

You're welcome.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
function checkBalance(thePlayer) 
    local account = getPlayerAccount ( thePlayer ) 
    local bankMoney = getAccountData ( account, "bankcash" ) 
    if ( bankMoney==false ) then 
        setAccountData ( account, "bankCash", 0 ) 
        bankMoney=0 
        outputChatBox ("hi") 
    else 
        outputChatBox ("EagleBot: " .. getPlayerName(thePlayer) .. " has $" .. bankMoney .. " in his bank!" ) 
    end 
end 
addCommandHandler ( "bank" , checkBalance ) 
  
function saveBalance() 
     
end 
addEventHandler ( "onPlayerQuit", getRootElement(), saveBalance ) 
  
function withdrawBalance() 
  
end 
  
function storeMoney( thePlayer, command, cash) 
    local account = getPlayerAccount ( thePlayer ) 
    local bankMoney = getAccountData ( account, "bankcash" ) 
    local store = bankMoney+cash 
    takePlayerMoney ( cash ) 
    setAccountData ( account, "bankCash", store ) 
    outputChatBox ( "You stored $" .. cash .. "in your bank!", thePlayer) 
end 
addCommandHandler ( "store" , storeMoney ) 

[2012-07-25 14:44:21] ERROR: [roleplay]\bank\server.lua:26: attempt to perform arithmetic on local 'bankMoney' (a boolean value)

why am i getting an error?

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted

You might have had an empty account data "bankMoney". Check if it saved correctly in your internal.db or by debug messages:

function storeMoney( thePlayer, command, cash) 
    local account = getPlayerAccount ( thePlayer ) 
    local bankMoney = getAccountData ( account, "bankcash" ) 
    local store = bankMoney+tonumber(cash) -- Convert string to number here. It might cause an error too. 
    outputChatBox("DEBUG: bankMoney is: "..tostring(bankMoney)) 
    takePlayerMoney ( cash ) 
    setAccountData ( account, "bankCash", store ) 
    outputChatBox ( "You stored $" .. cash .. "in your bank!", thePlayer) 
end 
addCommandHandler ( "store" , storeMoney ) 

Posted
You might have had an empty account data "bankMoney". Check if it saved correctly in your internal.db or by debug messages:
function storeMoney( thePlayer, command, cash) 
    local account = getPlayerAccount ( thePlayer ) 
    local bankMoney = getAccountData ( account, "bankcash" ) 
    local store = bankMoney+tonumber(cash) -- Convert string to number here. It might cause an error too. 
    outputChatBox("DEBUG: bankMoney is: "..tostring(bankMoney)) 
    takePlayerMoney ( cash ) 
    setAccountData ( account, "bankCash", store ) 
    outputChatBox ( "You stored $" .. cash .. "in your bank!", thePlayer) 
end 
addCommandHandler ( "store" , storeMoney ) 

still not working, i tried it just now.

[2012-07-25 15:59:01] ERROR: [roleplay]\bank\server.lua:26: attempt to perform arithmetic on local 'bankMoney' (a boolean value)

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted (edited)
function checkBalance(thePlayer) 
    local account = getPlayerAccount ( thePlayer ) 
    local bankMoney = getAccountData ( account, "bankcash" ) 
    if ( bankMoney==false ) then 
        setAccountData ( account, "bankCash", 0 ) 
        bankMoney=0 
        outputChatBox ("hi") 
    else 
        outputChatBox ("EagleBot: " .. getPlayerName(thePlayer) .. " has $" .. bankMoney .. " in his bank!" ) 
    end 
end 
addCommandHandler ( "bank" , checkBalance ) 
  
function saveBalance() 
     
end 
addEventHandler ( "onPlayerQuit", getRootElement(), saveBalance ) 
  
function withdrawBalance() 
  
end 
  
function storeMoney( thePlayer, command, cash) 
    local account = getPlayerAccount ( thePlayer ) 
    if isGuestAccount ( account ) then return not outputChatBox ( "* Invalid account.", thePlayer, 255, 0, 0 ) end 
    local bankMoney = getAccountData ( account, "bankcash" ) or 0 
    local store = bankMoney+cash 
    takePlayerMoney ( cash ) 
    setAccountData ( account, "bankCash", store ) 
    outputChatBox ( "You stored $" .. cash .. "in your bank!", thePlayer) 
end 
addCommandHandler ( "store" , storeMoney ) 

Edited by Guest

Retired

Posted

Obviously your account data 'bankMoney" returns false. It means that there's no any data saved in your internal database. You should check that there's something in it or any script makes it set correctly.

Posted

Ok, i fixed that using

serverside

  
function storeMoney( thePlayer, command, cash) 
    local account = getPlayerAccount ( thePlayer ) 
    if isGuestAccount ( account ) then return not outputChatBox ( "* Invalid account.", thePlayer, 255, 0, 0 ) end 
    if getPlayerMoney(thePlayer) >= tonumber(cash) then 
        local bankMoney = getAccountData ( account, "bankCash" ) or 0 
        local store = bankMoney+cash 
        takePlayerMoney ( thePlayer, cash ) 
        setAccountData ( account, "bankCash", store ) 
        outputChatBox ( "DEBUG: " .. store ) 
        outputChatBox ( "You stored $" .. cash .. " in your bank!", thePlayer) 
    else 
        outputChatBox ( "You do not have $" .. cash ..", you have $" .. getPlayerMoney(thePlayer) .. " and are short by $" .. cash-getPlayerMoney(thePlayer), thePlayer) 
    end 
end 
addCommandHandler ( "store" , storeMoney ) 
  

and I want to know how i can pass arguements to the serverside from clientside

clientside

  
local screenX, screenY = guiGetScreenSize() 
local width, height = 331,140 
local x = (screenX/2) - (width/2) 
local y = (screenY/2) - (height/2) 
local lp = getLocalPlayer() 
  
bankwindow = guiCreateWindow(x,y,331,140,"Bank System",false) 
balancelbl = guiCreateLabel(18,31,170,20,"Your Balance :",false,bankwindow) 
cashlbl = guiCreateLabel(18,61,170,20,"Your Cash :",false,bankwindow) 
worktxt = guiCreateEdit(22,95,158,27,"",false,bankwindow) 
storebtn = guiCreateButton(186,93,52,31,"Deposit",false,bankwindow) 
withdrawbtn = guiCreateButton(248,93,70,31,"Withdraw",false,bankwindow) 
guiWindowSetSizable(bankwindow,false) 
guiSetVisible (bankwindow, false) 
  
function resourceStart () 
  bindKey ("j", "down", menuShow) 
end 
addEventHandler ("onClientResourceStart", getResourceRootElement ( getThisResource () ), resourceStart) 
  
function menuShow () 
    visableornot = guiGetVisible (bankwindow) 
    if (visableornot == true) then 
        guiSetVisible (bankwindow, false) 
        showCursor (false) 
    end 
    if (visableornot == false) then 
        guiSetVisible (bankwindow, true) 
        showCursor (true) 
    end 
end 
  
function guiClick (button, state, absoluteX, absoluteY) 
  if (source == storebtn) then 
    triggerServerEvent ("withdraw", lp) 
  elseif (source == withdrawbtn) then 
    triggerServerEvent ("deposit", lp) 
  end 
end 
  
addEventHandler("onClientRender",root, 
    function() 
        dxDrawText("$01234567",1080.0,159.0,1281.0,214.0,tocolor(37,110,37,255),1.8,"pricedown","left","top",false,false,false) 
    end 
) 
  

so when i click store, it sends the amount of cash entered in the editbox to the server so i can use my function that i made, how do I do this?

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted

You have to create a function server-side which receives arguments from client-side. In client-side function you have to pass required arguments. Something like this:

triggerServerEvent ("withdraw", lp, cash) -- All needed arguments can be passed after lp (local player which will be 'source' of the server function. 'cash' might be your argument representing your money to be received from client.) 

Posted

would this work if i wanted to send the bankmoney to the clientside???

server side

  
triggerClientEvent( "menuShow", getRootElement(), cash ) 
  

clientside

  
addEvent("menuShow", true ) 
function menuShow ( thePlayer, cash ) 
    visableornot = guiGetVisible (bankwindow) 
    if (visableornot == true) then 
        guiSetVisible (bankwindow, false) 
        showCursor (false) 
    end 
    if (visableornot == false) then 
        guiSetText ( balancelbl, "Your Balance : $" .. cash ) 
        guiSetVisible (bankwindow, true) 
        showCursor (true) 
    end 
end 
addEventHandler("menuShow", getRootElement(), menuShow) 
  

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted

I'll explain it via comments:

Server side:

  
triggerClientEvent( "menuShow", getRootElement(), cash ) -- Instead of getRootElement() you should define your specific player if you want to show it to him. Root element will show you window to everyone. 'cash' is your money argument. It can be whatever you want. 
  

Client side:

  
addEvent("menuShow", true ) 
function menuShow ( thePlayer, cash ) -- 'thePlayer' is unnecessary variable here. It should be just menuShow ( cash ). 2nd argument from server-side will be 'source' of your client-side function so it's not required here. Leave here just 'cash'. 
    visableornot = guiGetVisible (bankwindow) 
    if (visableornot == true) then 
        guiSetVisible (bankwindow, false) 
        showCursor (false) 
    end 
    if (visableornot == false) then 
        guiSetText ( balancelbl, "Your Balance : $" .. cash ) 
        guiSetVisible (bankwindow, true) 
        showCursor (true) 
    end 
end 
addEventHandler("menuShow", getRootElement(), menuShow) -- I use here getLocalPlayer() as a rule. 
  

Posted

OK i got it all working, but i dont understand why this isnt working, the buttons arent even outputting the text

function guiClick (button, state, absoluteX, absoluteY) 
  local cash = guiGetText (worktxt) 
  if (source == storebtn) then 
    outputChatBox("should work...") 
    triggerServerEvent ("withdraw", lp, cash) 
  elseif (source == withdrawbtn) then 
    outputChatBox("should work...") 
    triggerServerEvent ("deposit", lp, cash) 
  end 
end 

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted

There's no event for your function. It should be like this:

addEventHandler("onClientGUIClick",getRootElement(), 
    function () 
  local cash = guiGetText (worktxt) 
  if (source == storebtn) then 
    outputChatBox("should work...") 
    triggerServerEvent ("withdraw", lp, cash) 
  end 
  if (source == withdrawbtn) then 
    outputChatBox("should work...") 
    triggerServerEvent ("deposit", lp, cash) 
  end 
    end) 

Posted
There's no event for your function. It should be like this:
addEventHandler("onClientGUIClick",getRootElement(), 
    function () 
  local cash = guiGetText (worktxt) 
  if (source == storebtn) then 
    outputChatBox("should work...") 
    triggerServerEvent ("withdraw", lp, cash) 
  end 
  if (source == withdrawbtn) then 
    outputChatBox("should work...") 
    triggerServerEvent ("deposit", lp, cash) 
  end 
    end) 

thanks to you i successfully made it, you are like a brother :D but just one more thing, you know how when you add a handler as onResourceStart, why does it do it when other resources start, can it do it only when the same resource starts?

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

Posted

You mean this?

addEventHandler( "onClientResourceStart", getRootElement( ), 
    function ( startedRes ) 
    if startedRes==getThisResource() then 
              -- Some stuff... 
    end 
    end 
) 

Posted
You mean this?
addEventHandler( "onClientResourceStart", getRootElement( ), 
    function ( startedRes ) 
    if startedRes==getThisResource() then 
              -- Some stuff... 
    end 
    end 
) 

this can be used for server side too?

Eat My Dust - DM/Deathmatch

350x20_00C3FF_FFFFFF_000000_000000.png

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...