[C#] TriggerClientEvent problem

Hello!
I need some help…

I need to be able to trigger client events for the sending client. For example, if i type /addgun i need the server to trigger an event which displays “Gun added” (for example) if the criteria is met… I have a working script at the moment but the server script triggers the same command for every client… So everyone gets a message saying “Gun Added” even if they didn’t send the command in chat…

Does that make sense?

My code is as follows;

Public Myscript()
{
EventHandlers.Add("chatMessage", new Action<string, string, string>(ChooseFunction));
 }

        public void ChooseFunction(string ID, string playerName, string Message)
        {


            if (Message == "/addgun")
            {
               
                CitizenFX.Core.Native.API.CancelEvent();
                TriggerClientEvent("NotifyFailure", "Need to specify a gun...");
            }

ID, playerName and Message are all specific to the player who sent the command in chat…
I have tried changing the TriggerClientEvent to;

TriggerClientEvent(ID, "NotifyFailure", "Need to specify a gun...");

but it still does not work… Any ideas???

Thanks,

Matt

You are not specifying what player the TriggerClientEvent is going to.

You will need to specify a Player object as the first argument in TriggerClientEvent to trigger on a specific player.

In order to get the player object from the source ID, you will need to do this

PlayerList pl = new PlayerList();
Player p = pl[ply];

ply is the source ID. p is your player object of the player who typed in the chat.

So changing it from

EventHandlers.Add("chatMessage", new Action<string, string, string>(ChooseFunction));

to;

EventHandlers.Add("chatMessage", new Action<Player, string, string>(ChooseFunction));

or just
changing .


TriggerClientEvent(ID, "NotifyFailure", "Need to specify a gun...");

to

PlayerList pl = new PlayerList();
Player p = pl[ply];
TriggerClientEvent(p, "NotifyFailure", "Need to specify a gun...");

???

Something like that.

edit:

No… keep the eventhandler the same. For some reason chatMessage only gets the player ID, not the object.

Don’t listen to @Vespura, the player argument needs to go first. :slight_smile:

1 Like

@Vespura @Briglair

This is the full code… well not the full code but if i get this working then i can get the rest working…

public InventoryServer()
        {
            Debug.WriteLine("Inventory by MFLW has successfully loaded");
            Inventory.Clear();
            EventHandlers.Add("playerConnecting", new Action<Player, string, CallbackDelegate>(OnPlayerConnecting));
            EventHandlers.Add("chatMessage", new Action<string, string, string>(ChooseFunction));
            
           
        }

        public void ChooseFunction(string ID, string playerName, string Message)
        {
            

            if (Message == "/additem")
            {
                //Debug.WriteLine("Passed if Additem");
                CitizenFX.Core.Native.API.CancelEvent();
                PlayerList pl = new PlayerList();
                Player p = pl[ID];
                TriggerClientEvent(p, "NotifyFailure", "You must enter at least one item");
            }

That throws this error…

Error invoking callback for event chatMessage: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
  at CitizenFX.Core.BaseScript.TriggerClientEvent (CitizenFX.Core.Player player, System.String eventName, System.Object[] args) [0x00000] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore\BaseScript.cs:117
  at InventoryServer.InventoryServer.ChooseFunction (System.String ID, System.String playerName, System.String Message) [0x00025] in <f05d232ee8fd46dab78040e014b98102>:0
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
   --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00048] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
  at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000e7] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
  at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00008] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
  at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
  at CitizenFX.Core.EventHandlerEntry+<Invoke>d__5.MoveNext () [0x00064] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore\EventHandlerDictionary.cs:85

Sorry about this…

Try changing

EventHandlers.Add("chatMessage", new Action<string, string, string>(ChooseFunction));

to

EventHandlers.Add("chatMessage", new Action<int, string, string>(ChooseFunction));

And change the ID in your function to int also

I believe that works… Im going to fill the rest of my code with that and try it out!

Thank you very very much in advance! Really appreciate it! Ive spend weeks on this script!

1 Like