Jump to content

triggerClientEvent


Recommended Posts

Posted

Hello guys, i have a little problem, when i kill a zombie, It appears a text showing how much i earned, but it appear for all players, and it suppose to appear only for who killed the zombie, here is my code:

Server

addEvent( "onZombieWasted" , true) 
addEventHandler( "onZombieWasted", getRootElement(), 
function( killer ) 
    local money = 500 
        givePlayerMoney( killer, tonumber(money)) 
        triggerClientEvent("onClientZombieWasted", killer, money) 
end) 

Client

local x,y = guiGetScreenSize() 
  
function onTest() 
        dxDrawText("+"..tostring(money), x*1211/1366, y*51/768, x*1351/1366, y*77/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1211/1366, y*49/768, x*1351/1366, y*75/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1209/1366, y*51/768, x*1349/1366, y*77/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1209/1366, y*49/768, x*1349/1366, y*75/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1210/1366, y*50/768, x*1350/1366, y*76/768, tocolor(22, 131, 38, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
         
--for i=1, 18 do 
--dxDrawText("A zombie got killed ".. i, x*0.2, 0 - y*0.03 + (y*0.05 * i),x,y,tocolor(255,255,255,255)) 
--end 
  
end 
  
  
addEvent("onClientZombieWasted", true) 
addEventHandler( "onClientZombieWasted", root, 
function(mon) 
    if not isTimer(moneyTimer) then 
        moneyTimer = setTimer(function() 
            removeEventHandler("onClientRender", root, onTest) 
        end, 2000, 1) 
        money = mon 
        addEventHandler("onClientRender", root, onTest) 
    else 
        money = (money + mon) 
        resetTimer(moneyTimer) 
    end 
end) 

Posted

uhm try defining the "killer"

Client:

local attacker = getPlayerName(  killer )   
local playeraccount = getPlayerAccount ( killer ) 

ofc you can change local and add it to server side if you want

Posted

The first argument in the triggerClientEvent is optional and is the element it will get sent to. Its root by default so just put killer before the name of the event and that should do it.

Posted

oh yes, didn't check the trigger client event correctly, as MIKI said should be like this

triggerClientEvent( killer, "onClientZombieWasted", money) 

Posted

-- no Need for local Money on Server . .

---ClinetSide ,

  
function onTest() 
local x,y = guiGetScreenSize() 
local money = getPlayerMoney (localPlayer) 
        dxDrawText("+"..tostring(money), x*1211/1366, y*51/768, x*1351/1366, y*77/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1211/1366, y*49/768, x*1351/1366, y*75/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1209/1366, y*51/768, x*1349/1366, y*77/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1209/1366, y*49/768, x*1349/1366, y*75/768, tocolor(0, 0, 0, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false) 
        dxDrawText("+"..tostring(money), x*1210/1366, y*50/768, x*1350/1366, y*76/768, tocolor(22, 131, 38, 255), 1.00, "pricedown", "left", "top", false, false, true, false, false)        
for i=1, 18 do 
dxDrawText("A zombie got killed ".. i, x*0.2, 0 - y*0.03 + (y*0.05 * i),x,y,tocolor(255,255,255,255)) 
end  
end 
  
addEvent("onClientZombieWasted", true) 
addEventHandler( "onClientZombieWasted", root, 
function(mon) 
    if not isTimer(moneyTimer) then 
        moneyTimer = setTimer(function() 
            removeEventHandler("onClientRender", root, onTest) 
        end, 2000, 1) 
        money = mon 
        addEventHandler("onClientRender",root, onTest) 
    else 
        money = (money + mon) 
        resetTimer(moneyTimer) 
    end 
end) 

---ServerSide ,

addEvent( "onZombieWasted" , true) 
addEventHandler( "onZombieWasted", getRootElement(), 
function( attacker ) 
  if ( getElementType ( attacker ) == "player" ) and ( getElementType ( source ) == "ped" ) then 
           givePlayerMoney(attacker, 500) 
       triggerClientEvent(attacker, 'onClientZombieWasted', attacker) 
         end 
   end 
   ) 

Posted
The first argument in the triggerClientEvent is optional and is the element it will get sent to. Its root by default so just put killer before the name of the event and that should do it.

MIKI785 Thank you.

Max, I prefer doing it in my way.

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