[C#] TriggerClientEvent works only once

fivem-scripts
help
csharp

#1

Hello, I have a problem on my C# code. One serverside method with TriggerClientEvent is working corretly but anothers works only one for a payer.

Here is my code.

// Server side
public class GunSystemServer : BaseScript
 {
        public GunSystemServer()
        {   
            EventHandlers.Add("chatMessage", new Action<int, string, string>(HelpGun));
            EventHandlers.Add("chatMessage", new Action<int, string, string>(GiveAllGuns));
        }

// server-client works well
        private void HelpGun(int playerid, string playername, string message)
        {
            if ( message == "/helpgun" )
            {
                PlayerList pl = new PlayerList();
                Player player = pl[playerid];
                TriggerClientEvent(player, "HelpGun", helpmessage);
            }
        }

       // server side works, client side works only once
        private void GiveAllGuns(int playerid, string playername, string message)
        {
            if ( message == "/giveallguns" || message == "/gag" )
            {
                API.CancelEvent(); // tried with and without that
                PlayerList pl = new PlayerList();
                Player player = pl[playerid];

                TriggerClientEvent(player, "GiveAllGuns");
            }
        }
}

// client side
public class GunSystemClient : BaseScript
 {
     
        public GunSystemClient()
        {               
            EventHandlers.Add("HelpGun", new Action<string>(HelpGun));
            EventHandlers.Add("GiveAllGuns", new Action(GiveAllGuns));
        }

         // works well
        private void HelpGun(string helpmessage) {
            TriggerEvent("chatMessage", "HELP", new[] { 0, 0, 200 }, helpmessage);
        }

        // works only once at connection time of a player
        private void GiveAllGuns()
        {
             // only for ingame debugging
            TriggerEvent("chatMessage", "DB", new [] { 200, 0, 0 }, "GiveAllGuns Client was triggerd");
            Ped ped = new Ped(Game.PlayerPed.Handle);

            var weaponList = Enum.GetValues(typeof(WeaponHash));
            foreach (var item in weaponList)
            {
                Function.Call(Hash.GIVE_WEAPON_TO_PED, ped, Convert.ToInt32(item), 1000, false);
            }
        }
}

HelpGun() works always, GiveAllGuns() works only once for a player at connection session.
If he reconnect he can used it once again.

Have I forget something? Thank you for helping


#2

As for your actual problem, it may have to do with the deprecated way of triggering commands. You should use RegisterCommand server-side.

Server Side

API.RegisterCommand("giveallguns", new Action<int, List<object>, string>((source, arguments, raw) =>
{
    TriggerClientEvent(Players[source], "GiveAllGuns");
}), false);

Client side

EventHandlers.Add("GiveAllGuns", new Action(GiveAllGuns));

private void GiveAllGuns()
{
    TriggerEvent("chatMessage", "DB", new [] { 200, 0, 0 }, "GiveAllGuns Client was triggered");
    
    foreach(var item in Enum.GetValues(typeof(WeaponHash)))
    {
        API.GiveWeaponToPed(Game.PlayerPed, item, 1000, false, true);
    }
}

This should work, haven’t tested it. If you run into issues, let me know.

General Tips:

You don’t need to create a Player. Passing a string playername is also unnecessary in your example (not used).

Just define a Player argument in your EventHandler and grab it from the source like so:

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

private void GiveAllGuns([FromSource]Player player, string message)
{
    // Trigger the event with one of these two:
    player.TriggerEvent("GiveAllGuns");
    //or
    TriggerClientEvent(player, "GiveAllGuns");
}

Triggering the server event client-side

TriggerServerEvent("GiveAllGuns");

#3

Yes, it works. Thank you for your help :slight_smile:

I forgot to update my CitizenFX.Core.dll from client side :man_facepalming: and I didn’t find a useful explanation how call client-server event handling in the latest version.

I hope that this topic helps more user than just me.