Jump to content

[HELP] TriggerClientEvent


brocky

Recommended Posts

I have a problem with my code and I am trying to trigger a client event which task is to "Show a GUI", When you enter the marker it will check your team, if it is "PatrolWorkers" then "triggerClientEvent" and if the team is not the one we want, then output the message that "your not in the team". the problem is that everytime the gui doesn't appears and i get the message again and again when I enter the marker "your not in the team".

I didn't use the hitElement and matching dimension. Please use THAT too when you show the code

  
spawner = createMarker (268.06826782227, 1361.6530761719, 9.5859375, "cylinder", 5.0, 243, 241, 13 )  
  
function theSpawner() 
   if spawner == source then 
  team = getPlayerTeam(source) 
  if team == "PatrolWorker" then  
        triggerClientEvent("jobSpawner", root) 
  else   
      outputChatBox("You must be a Patrol Worker to use this marker") 
               end  
                     end  
                         end  
addEventHandler("onMarkerHit", root, theSpawner) 
  

Link to comment
I have a problem with my code and I am trying to trigger a client event which task is to "Show a GUI", When you enter the marker it will check your team, if it is "PatrolWorkers" then "triggerClientEvent" and if the team is not the one we want, then output the message that "your not in the team". the problem is that everytime the gui doesn't appears and i get the message again and again when I enter the marker "your not in the team".

I didn't use the hitElement and matching dimension. Please use THAT too when you show the code

  
spawner = createMarker (268.06826782227, 1361.6530761719, 9.5859375, "cylinder", 5.0, 243, 241, 13 )  
  
function theSpawner() 
   if spawner == source then 
  team = getPlayerTeam(source) 
  if team == "PatrolWorker" then  
        triggerClientEvent("jobSpawner", root) 
  else   
      outputChatBox("You must be a Patrol Worker to use this marker") 
               end  
                     end  
                         end  
addEventHandler("onMarkerHit", root, theSpawner) 
  

spawner = createMarker (268.06826782227, 1361.6530761719, 9.5859375, "cylinder", 5.0, 243, 241, 13 ) 
  
function theSpawner() 
   if spawner == source then 
  local team = getPlayerTeam(source) 
  if team == "PatrolWorker" then 
        triggerClientEvent("jobSpawner", root) 
  else   
      outputChatBox("You must be a Patrol Worker to use this marker") 
               end 
                     end 
                         end 
addEventHandler("onMarkerHit", getRootElement(), spawner, theSpawner) 

Link to comment
what do you mean by this?

  
write = Example the Trigger 
  

I think he misunderstood how to use the function :P

triggerClientEvent(elementToSendTo, "eventNameToTrigger", sourceElement, Argument1, Argument2) 

The first one can either be a player of your choice, by using for example getPlayerFromName. Or you can leave it empty which defaults to root - meaning every player currently on the server.

"eventNameToTrigger" - this is the event that you will be triggering. You create this event via addEvent and handle the event via addEventHandler.

sourceElement - is the source of the element, what triggered it. Usually it's fine to just leave this at resourceRoot.

Arguments - These are arguments you can pass on, such as variables or tables.

-- Client: 
function ExampleFunction_Client(Text1, Text2) -- Make sure you retrieve any data, they will act as local variables, you can name these whatever you want 
    -- Text1 = "Hello" 
    -- Text2 = "World" 
end 
addEvent("exampleEvent", true) -- Create the event, "exampleEvent". true means that it can be remotely triggered 
addEventHandler("exampleEvent", root, ExampleFunction_Client) -- Of course we need an event handler to handle the event, attach it to the function(s) you want 
  
  
-- Server: 
function ExampleFunction_Server() 
    triggerClientEvent(root, "exampleEvent", resourceRoot, "Hello", "World") -- This triggers for all players 
    triggerClientEvent("exampleEvent", resourceRoot, "Hello", "World") -- Same as above ^ 
    triggerClientEvent(getRandomPlayer(), "exampleEvent", resourceRoot, "Hello", "World") -- This triggers for a random player, and that player only 
end 

I've thought about maybe making a video tutorial explaining how to use these functions and communicate between Client & Server since it's a rather important aspect of scripting. It's really easy once you've done it a few times :)

Link to comment

So depending on the code that I posted will this work if i do .

  
triggerClientEvent(player, "jobSpawner", resourceRoot) 
  

Depening on my code, Will this code work if I add this line. and if yes

Then how will it recognize "player" as the player who triggered the event? Don't we have to define "player" somehow?

Can it be defined by the parameter of "onMarkerHit" like the "hitElement" one?

Link to comment
So depending on the code that I posted will this work if i do .

  
triggerClientEvent(player, "jobSpawner", resourceRoot) 
  

Depening on my code, Will this code work if I add this line. and if yes

Then how will it recognize "player" as the player who triggered the event? Don't we have to define "player" somehow?

Can it be defined by the parameter of "onMarkerHit" like the "hitElement" one?

There's a multitude of ways you can define what player to trigger the event for. You're absolutely right about onMarkerHit and hitElement, as the Wiki mentions;

hitElement: The element that hit the marker

This can be either a player, ped, or a vehicle. So to verify that it is a player element, you can get getElementType. Or you can check what player is driving the vehicle in which hit the marker. :)

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