Jump to content

[TUT]Triggering


manve1

Recommended Posts

What you do is make a script and you need to trigger something?

I bet your like 'How do I do it?'

Well, this will help you!

Triggering is a simple way of getting information from on side to another (( client to server OR server to client ))

well, If you was triggering from Client side to server side, your script should look like this:

|| Client Side ||

triggerServerEvent('whatever', localPlayer) 

|| Server Side ||

addEvent('whatever', true) 
addEventHandler('whatever', getRootElement(), 
function() 
--code 
end 
) 

I think that you will be thinking now 'What is he on about.'

Well, this script isn't hard to understand.

The 'triggerServerEvent' triggers the code in your function to server side,

The 'whatever' bit in 'triggerServerEvent' is the Event name,

The 'localPlayer' bit in 2nd argument of 'triggerServerEvent' is for the person(s) you are trying to trigger the function too.

IT is the same for the 'server side to client side', just with different elements on the 2nd argument.

After all, I forgot to mension that after the 2nd argument of 'triggerClientEvent' or 'triggerServerEvent',

you can add any arguments you like.

Hope this helped you understand triggering client and server side.

Ask for any questions.

(( sorry for any mistakes. ))

Link to comment
IT is the same for the 'server side to client side', just with different elements on the 2nd argument.

hm

function startTrigger ( player, _, text ) 
    if not tonumber ( text ) then return end 
    if tonumber ( text ) == 1 then 
        triggerClientEvent ( root, "outputMsg", player, 1 ); 
    elseif tonumber ( text ) == 2 then 
        triggerClientEvent ( player, "outputMsg", player, 1 ); 
    elseif tonumber ( text ) == 3 then 
        triggerClientEvent ( player, "outputMsg", root, 1 ); 
    end 
end 
addCommandHandler ( "msg", startTrigger ) 
  
function outputText ( tMsg ) 
    if tMsg == 1 then 
        outputChatBox ( "Hello, this Event will work for all players, but player who used this command is " .. getPlayerName ( source ), root, 255, 255, 255, true ); 
        outputChatBox ( "We can send message only for the player", source, 255, 255, 255, true ); 
    elseif tMsg == 2 then 
        outputChatBox ( "Hello, this Event will work only for player who were used this cmd / you are ", .. getPlayerName ( source ), source, 255, 255, 255, true ); 
        outputChatBox ( "Ofc this msg can see all players too / but our player is " .. getPlayerName ( source ), root, 255, 255, 255, true ); 
    else 
        outputChatBox ( "Hello, this Event will work only for player ( who used this cmd ) but source is root element ( getRootElement ( ) ) ", source, 255, 255, 255, true ); 
        outputChatBox ( "Now we not have player ( Who used this command ), but this message can see all players ", source, 255, 255, 255, true ); 
    end 
end 
addEvent( "outputMsg", true ) 
addEventHandler( "outputMsg", root, outputText ) 

correct me if i'm wrong

and wiki said https://wiki.multitheftauto.com/wiki/TriggerClientEvent

sendTo: The event will be sent to all players that are children of the specified element. By default this is the root element, and hence the event is sent to all players. If you specify a single player it will just be sent to that player.

so you can use

triggerClientEvent ( "outputMsg", player, 1 ); 

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...