[HELP] C# TriggerClientEvent not working

Hello guys, I’m trying to trigger an event from the client when a player connects

Server (this works, the debug lines are shown):

private void OnPlayerConnecting(string playerName)
        {
            Debug.WriteLine("[SERVER] Player " + playerName + " connected!");
            TriggerClientEvent("startPlayer");
            Debug.WriteLine("[SERVER] Triggered client event startPlayer");
        }

Client:

public Main()
        {
            Tick += OnTick;
            EventHandlers["startPlayer"] += new Action(StartPlayer);
            Debug.WriteLine("Registered startPlayer event");
        }

        public async Task OnTick()
        {

        }

        public void StartPlayer() //it doesn't get called I don't know why!
        {
            Debug.WriteLine("[CLIENT] Triggered startPlayer event"); 
            XmlData.PlayerDataManager pdm = new XmlData.PlayerDataManager(Game.Player);
        }

TriggerClientEvent takes a second parameter, the player to trigger the event on.

I believe you can get this by doing

OnPlayerConnecting([FromSource] Player source, string playerName)

I don’t know how the C# TriggerClientEvent works but, try passing the player second. If that doesn’t work, try passing the player’s ID (probably called Player.ID)

It would look something like

TriggerClientEvent("event", source);
1 Like

On the client side you wouldn’t do it like that, it would be like so:


public Main() {
    this.EventHandlers.Add("startPlayer", new Action(this.StartPlayer));
}

private void StartPlayer() {
    // your code here
}
1 Like

Ah, thanks I was missing the “[FromSource]” didn’t know how to use :stuck_out_tongue:
So I was crying all the day because I couldn’t find a way to get the player from the “playerConneting” event and now I know after I changed everything lol.
Thanks by the way :laughing:

Apparently, the entire clientside script doesn’t work… if I use Debug.WriteLine(); in the constructor it doesn’t show up I don’t know why…
in the __resource.lua there’s written "client_script “nfsv_client.net.dll” and still doesn’t work, why?

i’m not agree the way he wrote is good , i write it like him and i had no problem.

Same, [FromSource] is the way out!

if it doesn’t work , have you call your trigger on serveur side like this : TriggerClientEvent(player,“your event”, yourParamIfOne);

Exactly what @304bl said. EventHandlers["startPlayer"] += new Action(StartPlayer); Should work just fine.

1 Like

don’t forget to check the function under visual studio , for TriggerClientEvent , the player is the first param , second is command , and other can be params

guys it works thanks ahahah