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

Thank you very much. I needed this Tut

c:

Can somebody Help me out with this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CitizenFX.Core;

namespace LOMCoreServer
{
    public class LOMCoreServer : BaseScript
    {
        public static String MOTD = "Willkommen!";
        public static String prefix = "[LOM-Core // Server]";

        public LOMCoreServer()
        {
            EventHandlers.Add("chatMessage", new Action<int, string, string>(onPlayerText));
            EventHandlers.Add("playerConnecting", new Action<Player, string, CallbackDelegate>(OnPlayerConnecting));
        }

        private void onPlayerText([FromSource] int player, string playerName, string chatMessage) 
        {
            Debug.WriteLine($"{prefix} -CHAT- ({player}): {chatMessage}");
        }

        private void OnPlayerConnecting([FromSource] Player player, string playerName, CallbackDelegate kickReason)
        {
            TriggerClientEvent......
            Debug.WriteLine($"{prefix} {player.Name} versucht zu verbinden");
            
        }
    }
}

I dont know, but if i want to use the ‘TriggerClientEvent’ in the OnPlayerConnectingVoid Method, VS2017 says it is not availble.

I already double-checked my CitizenFX.Core.dll.

1 Like

The “Player” class has a function in it to trigger events. So, in your function I believe you would write player.TriggerEvent.

2 Likes

This doesnt want to work too. :frowning:

Hello All.
I am having the same issue as many others. Server code is fine, Client code is fine but Server to Client communication doesn’t work. I have even copy-pasted the code from this tut.
I have checked this thread and copy-pasted the code: Please post some c# code snippets
Even from there no Server to Client code works and “playerConnecting” seems to be the only Server Event that works.

For what do you need the “Shared” project?

Shared project are all the functions and data that are common between client and server. You can just have a server and client project without using a “shared” project.

2 Likes

Just a heads up, while the guide works for getting started someone actually following the guide will not yield a MOTD.

As mentioned before, player connecting is handled on initial server connection before chat box is available. Any attempts to replicate this code will not work because of this.

That being said, is there a way for the server to call an action on successful spawn? Or do i have to trigger a server event on spawn to get the MoTD, then trigger that message again on the client?

In the latter case, the mini guide should express that the user should request a server action, where the server action requests a user action providing the MoTD details.

For the client :

private bool FirstSpawn = true;

EventHandlers.Add("sendMotd", new Action<string>(ReceivedMotd));
EventHandlers.Add("playerSpawned", new Action<Vector3>(RequestMotd));

 private void ReceivedMotd(string motd)
        {
            TriggerEvent("chatMessage", "SYSTEM", new[] { 255, 0, 0 }, motd);
        }
 private void RequestMotd([FromSource]Vector3 pos)
        {
            Debug.WriteLine("PlayerSpawn");
            if (FirstSpawn)
            {
                FirstSpawn = false;
                TriggerServerEvent("requestMotd");
            }  
        }

Server:

 string motd = "Don't Trust OP, The cake is a truth.";

var requestMotd = new Action<Player>(RequestMotd);
EventHandlers.Add("requestMotd", requestMotd);

 private void RequestMotd([FromSource]Player Player)
        {
            TriggerClientEvent(Player, "sendMotd", motd);
            Debug.WriteLine($"{Player.Name} requested the motd");
        }
2 Likes

I realized that this issue occurs, but havent gotten around on re-writing the tutorial.

Once I have more time, I think I will write a “from-zero-to-hero” guide for C# developement.

This was a quick write up back then and I was also new to all of this (and the FiveM API it self).

2 Likes

No worries, might want to add a “Editors Note” at the top of the post :slight_smile: i just got here from google myself and was confused about what was happening.

I did a quick edit, so the code atleast works :smiley:

1 Like

Does playerSpawn fire from the server? I couldn’t get it to work myself, thus why I had it firing from the client.

The spawnmanager should trigger that event upon respawning.

1 Like

I will give it a shot and edit this!

Firstly

 private void OnPlayerSpawned([FromSource]Vector3 pos)
        {
            TriggerClientEvent(player, "sendMotd", Motd);
        }

isn’t going to work because we removed the player variable from the constructor.

Havent tested this code because i do not have succes starting up a C# project something is wrong on my side, not clear what it is, but i think this will get your player.
player = GetPlayerPed(PlayerId()).ToString();

I have had wonderful success with C#, this guide helped me get started. Now pushing over 1k lines of code in the past couple days.

LocalPlayer is how you want to get the player on the client side :wink:

i will try that out to night, i was mixing up common .lua method with this, i was putting the dll’s in server.lua and client.lua, i was too frustrated to realise it had to go in __resource.lua… i am playing around with SetTimeScale(0.3f) bullet time effect. i wonder what it will do with more players in it.

If you are making a c# plugin, you don’t need anything other than the resource file and the Server.net.dll and Client.net.dll.

Keep in mind you need to have them named .net.dll for them to load properly.

1 Like

If you are not happy how this forum is moderated, then make a complaint. Please keep it out of my topic.
@Pater429

Nice to make a tutorial for something like this to get people started.