Jump to content

addEventHandler("onMoneyChange",..


BriGhtx3

Recommended Posts

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 :/

Link to comment

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

Link to comment
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.

Link to comment

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
Link to comment

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 

Link to comment

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.

Link to comment
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.

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