[How-to] Setting up C# and creating a basic resource

I’m having the same problem as Wolfinator,
Server code runs fine, but it seems that the client code isn’t even executing…
Very particular… Is there any who can help~?

If you have a very simple code like this:

// Constructor
public Client()
{
    Tick += OnTick
}

public async Task OnTick()
{
    Debug.WriteLine("Hello Client")
    await Delay(100)
}

It should spam the client console with “Hellow Client”. If this doesn’t work you either have a fault in the resource manifest or the namespace is wrong.

Thanks! That worked perfectly! I guess the problem is when sending the info from the server to the client, the client doesn’t receive it… Hmm…

Probably because you Trigger an event in the constructor, that doesn’t work afaik.

I see, where should I add the Trigger then? I could do it in the first tick using a Boolean, But I feel like there might be a better way… Oh, and I couldn’t find any easy way of checking for player input (keyboard keys) and I also don’t know how to get the player’s position… Sorry, I’m a bit new to FiveM development. ^~^
Thanks you for your help, it’s much appreciated!

1 Like

@Syntasu Thank you so much for writing this. This is a lot of useful information, especially for someone like me, who is no stranger to C#, but new to FiveM. :slight_smile:

1 Like

Glad my guide could be of any help to you!

Thx you very much Syntasu, but I have a question… the Server-Client Call does work ?
Because if I try “TriggerClientEvent” doesn’t work… only from client-server the triggers seems to work

TriggerClientEvent should work fine, you just need to pass the client id.

int clientID = 1 //some client id
TriggerClientEvent("eventName", clientIdD, "some_data");

Client Id can be obtained by using the [FromSource] attribute on incoming events.

Thx very much for your fast answer, I tried that now:

SERVER SIDE

 private void OnPlayerConnecting(string playerName, CallbackDelegate kickReason)
      {
          TriggerClientEvent("sendMotd", 1, Motd);
          
           Debug.WriteLine($"{playerName} sending message to the client ");
        }

On the console of the server I see the message “sending message to the client”.

CLIENT SIDE

       public BasicResourceClient()
        {
            Tick += OnTick;
            EventHandlers["sendMotd"] += new Action<string>(ReceivedMotd);

        }

        private void ReceivedMotd(string motd)
        {
            Debug.WriteLine("Client received the message");   
        }

But in this case I can’t see inside the game console the message “Client received the message” … :confused:

I tried with and whidout the 1.

Hmm try using ID -1, This will broadcast it to all the clients.


nothing using -1 …


I tested also to see if the client is working and if I put that code:
public BasicResourceClient()
{
Debug.WriteLine(“Client received the message”);
}

The message appear in the console (work, soo the client is running).

C# is different with the triggerclientevents.

If follows these paremeters TriggerClientEvent(Player player, String eventName, Args...)

For it to trigger on all clients, just don’t fill in the Player, and do TriggerClientEvent(String eventName, Args...)

TriggerClientEvent("sendMotd", Motd);

The example I did is without the player and doesn’t work (using just String eventName, Args) … I don’t know why :/…
Tried also with the player… and doesn’t work also in this case…

Have they fully implemented this in C#? Or I’m doing something wrong?


I tried also to use something like that just to test :

      public BasicResourceClient()
        {

            Tick += OnTick;
            EventHandlers["sendMotd"] += new Action<string>(ReceivedMotd);
            TriggerEvent("sendMotd", "test");
        }

This triggerevent should call the EventHandlers itself right? But also this doesn’t work…
Now could be the EventHandlers in the client side the real problem?

Triggering events from the constructor won’t work.

Ok, but the problem remain why also that dosn’t work… :confused:

 private void OnPlayerConnecting(string playerName, CallbackDelegate kickReason)
      {
          TriggerClientEvent("sendMotd", Motd);
          
           Debug.WriteLine($"{playerName} sending message to the client ");
        }

P.S. I tested the script you posted and have the same problem…just for the server-client call.

On playerConnecting, the player won’t exist yet, so if you want to send an event to that specific player, you’ll need an event that executes a bit later.

Thx titanium, it’s working , tested with EventHandlers[“chatMessage”] += new Action<int, string, string>(OnPlayerText); .
Thx also to everyone that answered and Syntasu for the tutorial without I could never start scripting :)!

Now I will try to port my single player zombie mod, thx very much again guys.

Exactly the kind of guide I was waiting for! Thank you! I am used to C-Style languages like C++ and C# and really don’t like LUA scripting styles (To me it just looks like spaghetti).

Again, awesome guide. Thank you so much!

4 Likes

No kidding. I write C# code for a living, so it’s just what I’m used to.

There’s no documentation on the C# side of things, so threads like this are a huge help.