[C#] server/client events

Okay so I have tried to mess around with communication between server and client and I cannot seem to get it to work.

it the contructor of the client I got:

 EventHandlers.Add("sendstring", new Action<string>(SpamConsole));

then the following function:

  private void SpamConsole(string message)
        {
            for(int x = 0; x < 50; x++)
            {
                Debug.WriteLine(message);
            }        
        }

server constructor:
EventHandlers.Add(“playerConnecting”, new Action<Player, string>(OnPlayerConnecting));

OnPlayerConnecting function

 private void OnPlayerConnecting([FromSource]Player player, string playerName)
        {
            TriggerClientEvent("sendstring", "--------------SpamConsole for testing--------------");
            Debug.WriteLine($"{player.Name} triggers client event");
        }

The onplayerconnecting function is done I can tell in the console. But I after connecting and I check the console I never seen the like 50 messages it should spam.

Trigger client event on who? Who is the server sending this event to?

Edit: if you didn’t get that. You’re not telling the function who to send the event to. I believe you can do player.TriggerEvent or player.TriggerClientEvent

Good point lol, I changed it however it still doesn’t show up in my console, so either I am blind or there is still something wrong

I don’t think you can trigger an event on a connecting player, because they aren’t fully in the game yet. I’ve tried and had issues.

Your genius, I got it to work doing it like so:

        private async void OnPlayerConnecting([FromSource]Player player, string playerName)
        {
            await Delay(10000);
            player.TriggerEvent("sendstring", "--------------SpamConsole for testing--------------");
            
            Debug.WriteLine($"{player.Name} triggers client event");
        }

But the event stuff is a little confusing, because it seems like there is 2 ways:

   EventHandlers.Add("eventname", new Action<Player>(somefunction));
   EventHandlers["eventname"] += new Action<Player>(somefunction);

and can you only use those for existing events or can u make custom ones?

edit

Figured it out, custom events work, now can finally have some fun :smiley: