Jump to content

triggerEvent question


Hero192

Recommended Posts

You can use this function to trigger your own events, such as:

-- server side:

addEvent ( "onHelloCommandUsed", true ) -- Register the 'onHelloCommandUsed' event 
addEventHandler ( "onHelloCommandUsed", root, 
    function ( ) 
        outputChatBox ( getPlayerName ( source ) .." used the /hello command." ) -- Output the name of the player that used the /hello command to the chat 
    end 
) 
  
addCommandHandler ( "hello", 
    function ( thePlayer ) 
        triggerEvent ( "onHelloCommandUsed", thePlayer ) -- Trigger the 'onHelloCommandUsed' event 
    end 
) 

Link to comment

So this event can be used only with the Register event of the trigger function? or it can be used on this situation too

triggerEvent ( "onPlayerShoot", player, enemy) 
 triggerClientEvent ( player, "onPlayerShoot", player, enemy) 

What I understood from you, This function get all the information from register event and sends it to somewhere by using triggerEvent, Correct me if I am wrong please

Link to comment

What I understood from you, This function get all the information from register event and sends it to somewhere by using triggerEvent, Correct me if I am wrong please

I know how to use the trigger(client/server)event , but I have a problem with this can you correct my first question please

Link to comment
If you register an event with addEvent function, then you can trigger it later with triggerEvent.

Okay thanks I understood,

So, The usage of this function is totally wrong , Because the event register doesn't exist here to use this event, RIGHT?

triggerEvent ( "onPlayerShoot", player, enemy) 
 triggerClientEvent ( player, "onPlayerShoot", player, enemy) 

Link to comment

It's rather simple, really. It works pretty much identically to that of triggerClientEvent and triggerServerEvent with one exception; it's one-sided.

triggerClientEvent - Executed in a server-side script, it triggers a client-side event. Server -> Client

triggerServerEvent - Executed in a client-sided script, it triggers a server-side event. Client -> Server

triggerEvent - Can be executed in either client- or server-sided scripts, triggers an event that is in the same environment as it was executed. Client -> Client OR Server -> Server

For example;

-- Server Side 
function ExampleEvent() 
    outputChatBox("This is my example event!") 
end 
addEvent("myExampleEvent") 
  
function TriggerCMD() 
    triggerEvent("myExampleEvent", root) -- This will trigger the above event 
end 
addCommandHandler("Trigger", TriggerCMD, false) 
  
-- Client Side 
function TriggerClientCMD() 
    triggerEvent("myExampleEvent", root) -- This will return an error, the event "myExampleEvent" does not exist 
end 
addCommandHandler("Trigger", TriggerClientCMD, false) 

Link to comment
It's rather simple, really. It works pretty much identically to that of triggerClientEvent and triggerServerEvent with one exception; it's one-sided.

triggerClientEvent - Executed in a server-side script, it triggers a client-side event. Server -> Client

triggerServerEvent - Executed in a client-sided script, it triggers a server-side event. Client -> Server

triggerEvent - Can be executed in either client- or server-sided scripts, triggers an event that is in the same environment as it was executed. Client -> Client OR Server -> Server

For example;

-- Server Side 
function ExampleEvent() 
    outputChatBox("This is my example event!") 
end 
addEvent("myExampleEvent") 
  
function TriggerCMD() 
    triggerEvent("myExampleEvent", root) -- This will trigger the above event 
end 
addCommandHandler("Trigger", TriggerCMD, false) 
  
-- Client Side 
function TriggerClientCMD() 
    triggerEvent("myExampleEvent", root) -- This will return an error, the event "myExampleEvent" does not exist 
end 
addCommandHandler("Trigger", TriggerClientCMD, false) 

Little mistake there, you forgot to attach the function to the event.

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