Jump to content

triggerClientEvent


Recommended Posts

Hello guys, i'm trying to trigger an dxText which it will show how much XP i earned after i kill someone, here is my code

Client:

sWidth,sHeight = guiGetScreenSize() 
function onTestExp(X) 
    dxDrawText("#00FF000 +"..tonumber(X), (539/1024)*sWidth, (745/768)*sHeight, (289/1024)*sWidth, (250/768)*sHeight, tocolor (255, 255, 255, 255), (0.7/1366)*sWidth,(0.7/768)*sHeight,"bankgothic","left","top",false,false,false,true)    
end 
      
addEvent("onTestExp", true) 
addEventHandler( "onTestExp", root, 
function() 
        addEventHandler("onClientRender", root, onTestExp) 
        setTimer( 
        function () 
                removeEventHandler ( "onClientRender", root, onTestExp ) 
        end, 3000, 1) 
end) 

Server:

--just some lines of the code 
        local X = math.random(5, 45) 
        triggerClientEvent("onTestExp", killer, X) 

And killer is defined here:

function win(killer, weapon, bodypart) 
    if (killer and getElementType(killer) == "player" and killer ~= source) then 

Link to comment

Example:

local experience = 0 -- Define global variable 

Now, in your trigger, you are sending the experience value, so, you must add the argument in the client side function:

addEvent ( "onTestExp", true ) 
addEventHandler ( "onTestExp", root, 
    function ( expe ) -- Here you are defining an argument, in this case, the value is what is sent from the server side. 
    end 
) 

So, all what you have to do next is set the "experience" variable value, which you can do it like this:

experience = expe 

After that, you add the event handler to render it, and in dxDrawText you use "experience" variable instead of "X".

Link to comment

Here is the client side part,

sWidth, sHeight = guiGetScreenSize() 
function onTestExp() 
    dxDrawText("#00FF000 +"..tostring(experience), (539/1024)*sWidth, (745/768)*sHeight, (289/1024)*sWidth, (250/768)*sHeight, tocolor (255, 255, 255, 255), (0.7/1366)*sWidth,(0.7/768)*sHeight,"bankgothic","left","top",false,false,false,true)   
end 
      
addEvent("onTestExp", true) 
addEventHandler( "onTestExp", root, 
function( expe ) 
experience = expe 
        addEventHandler("onClientRender", root, onTestExp) 
        setTimer( 
        function () 
                removeEventHandler ( "onClientRender", root, onTestExp ) 
        end, 3000, 1) 
end) 

I will send you the Server side in pm if it's possible..

Edited by Guest
Link to comment

Thanks Castillo.

And is it possible to do the following?

If i killed like example 2 people in the same time, it will count automatically how much i earned like example.

I killed the first one and i got 24 xp and after i kill the second one i got 22, so it will output 46 if you know what i mean, if it's not possible then never mind thanks.

Link to comment

Anything is possible if you know how to do it.

I would suggest to define your timer, so then you can use isTimer to know if the timer is still running, and if it is, you can reset it and increase the value of the "experience" variable with the value sent from the server side.

Link to comment
sWidth, sHeight = guiGetScreenSize() 
function onTestExp() 
    dxDrawText("#FFFFCC +"..tostring(experience), (539/1024)*sWidth, (545/768)*sHeight, (289/1024)*sWidth, (250/768)*sHeight, tocolor (255, 255, 255, 255), (0.7/1366)*sWidth,(0.7/768)*sHeight,"bankgothic","left","top",false,false,false,true)    
end 
      
addEvent("onTestExp", true) 
addEventHandler( "onTestExp", root, 
function( expe ) 
experience = expe 
        addEventHandler("onClientRender", root, onTestExp) 
        setTimer( 
        function () 
                removeEventHandler ( "onClientRender", root, onTestExp ) 
        end, 2000, 1) 
        if isTimer(source) then 
            resetTimer(source) 
            experience = ( experience + expe ) 
            setTimer( 
        function () 
                removeEventHandler ( "onClientRender", root, onTestExp ) 
        end, 2000, 1) 
        end 
end) 

Edited by Guest
Link to comment
sWidth, sHeight = guiGetScreenSize() 
function onTestExp() 
    dxDrawText("#FFFFCC +"..tostring(experience), (539/1024)*sWidth, (545/768)*sHeight, (289/1024)*sWidth, (250/768)*sHeight, tocolor (255, 255, 255, 255), (0.7/1366)*sWidth,(0.7/768)*sHeight,"bankgothic","left","top",false,false,false,true)    
end 
  
addEvent("onTestExp", true) 
addEventHandler( "onTestExp", root, 
function(expe) 
    if not isTimer(expTimer) then 
        expTimer = setTimer(function() 
            removeEventHandler("onClientRender", root, onTestExp) 
        end, 2000, 1) 
        experience = expe 
        addEventHandler("onClientRender", root, onTestExp) 
    else 
        experience = (experience + expe) 
        resetTimer(expTimer) 
    end 
end) 

Link to comment

I'm working on exp / level system.

Im make this but i have a problem.

Exp have been changed, must disappearing after 2500 milliseconds. But for the first time the changing exp triggers.

How to fix it?

local sWidth, sHeight = guiGetScreenSize () 
function onTestExp() 
    dxDrawText("+"..tostring(givePlayerExp), (1542/1680)*sWidth, (10/1050)*sHeight, (1594/1680)*sWidth, (29/1050)*sHeight, tocolor(255, 20, 0, 255), (0.60/1680)*sWidth, (0.60/1050)*sHeight, "bankgothic", "left", "top", false, false, true, true, false) 
end 
  
handlerExist = false 
addEvent("onTestExp", true) 
addEventHandler( "onTestExp", root, 
function( expe ) 
    givePlayerExp = expe 
     
    if (handlerExist == false) then 
    addEventHandler( "onClientRender", root, onTestExp ) 
    handlerExist = true 
    setTimer( function ()  
        if (handlerExist == true) then 
        removeEventHandler( "onClientRender", root, onTestExp ) 
        handlerExist = false 
        end 
end, 2500, 1 ) 
    else 
    removeEventHandler( "onClientRender", root, onTestExp ) 
    addEventHandler( "onClientRender", root, onTestExp ) 
    handlerExist = true 
    setTimer( function ()  
        if (handlerExist == true) then 
        removeEventHandler( "onClientRender", root, onTestExp ) 
        handlerExist = false 
        end 
end, 2500, 1 ) 
    end 
end 
) 

Sry for bad English.

Link to comment
sWidth, sHeight = guiGetScreenSize() 
function onTestExp() 
    dxDrawText("#FFFFCC +"..tostring(experience), (539/1024)*sWidth, (545/768)*sHeight, (289/1024)*sWidth, (250/768)*sHeight, tocolor (255, 255, 255, 255), (0.7/1366)*sWidth,(0.7/768)*sHeight,"bankgothic","left","top",false,false,false,true)    
end 
  
addEvent("onTestExp", true) 
addEventHandler( "onTestExp", root, 
function(expe) 
    if not isTimer(expTimer) then 
        expTimer = setTimer(function() 
            removeEventHandler("onClientRender", root, onTestExp) 
        end, 2000, 1) 
        experience = expe 
        addEventHandler("onClientRender", root, onTestExp) 
    else 
        experience = (experience + expe) 
        resetTimer(expTimer) 
    end 
end) 

Thanks TAPL. : )

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