Jump to content

addEventHandler("onMoneyChange",..


BriGhtx3

Recommended Posts

Posted

Is it possible to create something that when the amount of money changes, it automaticly saves it?

I know that I can use a timer, but I mean sth like a handler

Posted

Well you can make for yourself too ..

Here are is my suggestion :

onClientRender 

You use this as a event handler , for the function , and you must script that everytime it saves the amount of the money

Posted

Here

function DXdraw() 
sWidth, sHeight = guiGetScreenSize() 
dxDrawText("Money "..tostring (getPlayerMoney(getLocalPlayer()),40,40,sWidth,sHeight,tocolor(0,255,0,255),1.5,"pricedown","right","top",false,false,false) 
end 
addEventHandler("onClientRender", getRootElement(), DXdraw) 

Posted

New problem :

I'm trying to show the players country but it always returns false.

function setLand() 
    local flag = exports['admin']:getPlayerCountry ( source ) 
    outputChatBox(tostring(flag)) 
    if flag then 
        setElementData(source,"Land",":admin/client/images/flags/"..flag..".png") 
    else 
        setElementData(source,"Land","N/A") 
    end 
end 
addEventHandler("onPlayerJoin",getRootElement(), setLand) 

The country is always set to N/A. I'm living in Germany :/

Posted

Money doesn't change out of the blue..

Money only changes when you tell the script to use setPlayerMoney, givePlayerMoney or takePlayerMoney

Everytime you use either of those functions, launch your own custom event "onMoneyChange", eventually passing the current money or whatever you like as parameters.

use triggerEvent()

https://wiki.multitheftauto.com/wiki/TriggerEvent

E.g: In case you use givePlayerMoney(player, 300), do this afterwards: triggerEvent ('onMoneyChange', player, getPlayerMoney(player))

PS: You can scrap passing getPlayerMoney, I'm just showing an example

Posted
local oldMoney = getPlayerMoney() 
  
function renderMoney() 
    newMoney = getPlayerMoney() 
    if ( tonumber(newMoney) ~= tonumber(oldMoney) ) then 
         triggerEvent("onClientMoneyChange",localPlayer,oldMoney,newMoney) 
         oldMoney = newMoney 
    end 
end 
addEventHandler("onClientRender",root,renderMoney) 
  
addEvent("onClientMoneyChange",true) 
addEventHandler("onClientMoneyChange",root, 
function (old, new) 
    outputChatBox(tostring(old) ..": ".. tostring(new)) 
end) 

Seems to work just fine.

Posted (edited)

Client

addEvent( "onClientMoneyChange",true ) 
  
addEventHandler( "onClientMoneyChange",root, 
    function( oldMoney,newMoney ) 
        -- .... 
    end 
) 
     
function _givePlayerMoney( ... ) 
    local args = { ... } 
    if type( args[1] ) == "number" then 
        if triggerEvent( "onClientMoneyChange",localPlayer,getPlayerMoney( ),args[1] ) and 
        givePlayerMoney( ... ) then 
            return true 
        end 
        return false     
    end 
end 

Edited by Guest
Posted

This is example with client side.

Server

addEvent( "onMoneyChange",true ) 
  
addEventHandler( "onMoneyChange",root, 
    function( oldMoney,newMoney ) 
        -- .... 
    end 
) 
     
function _givePlayerMoney( ... ) 
    local args = { ... } 
    if isElement( args[1] ) and type( args[2] ) == "number" then 
        if triggerEvent( "onMoneyChange",args[1],getPlayerMoney( args[1] ),args[2] ) and 
        givePlayerMoney( ... )  
        then 
            return true 
        end 
        return false 
    end 
end 

Posted

Solidsnake, just out of curiosity, why did u add this line?

if (oldMoney < newMoney) then 

surely with that it only works if old money is less.. what if they have more?

shouldnt the function be:

function renderMoney() 
    newMoney = getPlayerMoney() 
    if ( tonumber(newMoney) ~= tonumber(oldMoney) ) then 
          triggerEvent("onClientMoneyChange",localPlayer,oldMoney,newMoney) 
          oldMoney = newMoney 
    end 
end 
addEventHandler("onClientRender",root,renderMoney) 

no offence intended... u do a great job on the forums helping people.

Posted
Solidsnake, just out of curiosity, why did u add this line?
if (oldMoney < newMoney) then 

surely with that it only works if old money is less.. what if they have more?

shouldnt the function be:

function renderMoney() 
    newMoney = getPlayerMoney() 
    if ( tonumber(newMoney) ~= tonumber(oldMoney) ) then 
          triggerEvent("onClientMoneyChange",localPlayer,oldMoney,newMoney) 
          oldMoney = newMoney 
    end 
end 
addEventHandler("onClientRender",root,renderMoney) 

no offence intended... u do a great job on the forums helping people.

Yup, it was a mistake, this was part of one of my HUD system's, I guess it was intended to do something for the HUD ;)

Thanks for finding it out.

Corrected the script.

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