PiTyUs.Hire me

Engineering

Writing FiveM resources in C#

Get started with C# for FiveM: the CitizenFX.Templates dotnet templates, client and server projects, BaseScript, EventHandlers, commands, calling natives through CitizenFX.Core.Native.API, async Delay, building to dist and linking into resources, and when C# makes sense.

Updated 10 min readBy PiTyUs · FiveM developer

Overview

C# is FiveM’s third scripting language, next to Lua and JavaScript. It brings static typing, a mature IDE (Visual Studio or Rider) and the .NET ecosystem. It is less common for roleplay scripts, but popular for larger systems and with developers who already know C#.

01Setting up

Command promptbat
dotnet new -i CitizenFX.Templates
mkdir MyResource
cd MyResource
dotnet new cfx-resource

You get a solution with a client and a server project. Run build.cmd to build release DLLs targeting the .NET version FiveM and FXServer expect; the output lands in dist, with an fxmanifest.

Link the build into your server’s resourcesbat
mklink /d C:\FXServer\server-data\resources\[local]\MyResource C:\dev\MyResource\dist

A client script

Client/Main.cscsharp
using System;
using System.Collections.Generic;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;

public class Main : BaseScript
{
    public Main()
    {
        EventHandlers["onClientResourceStart"] += new Action<string>(OnStart);
    }

    private void OnStart(string resource)
    {
        if (GetCurrentResourceName() != resource) return;

        RegisterCommand("coords", new Action<int, List<object>, string>((source, args, raw) =>
        {
            var pos = GetEntityCoords(PlayerPedId(), true);
            Debug.WriteLine($"{pos.X:F2}, {pos.Y:F2}, {pos.Z:F2}");
        }), false);
    }
}

Server events

Server/Main.cscsharp
using System;
using CitizenFX.Core;

public class Main : BaseScript
{
    public Main()
    {
        EventHandlers["fishmarket:sell"] += new Action<Player>(OnSell);
    }

    private void OnSell([FromSource] Player player)
    {
        Debug.WriteLine($"{player.Name} wants to sell fish");
        // validate on the server, then pay
    }
}

[FromSource] Player gives you the calling player. Waiting is done with await Delay(ms) inside async methods — never block the thread. Validation rules are the same as for any language: secure server events.

When C# makes sense

  • You or your team already work in C#/.NET.
  • Large systems where static types and refactoring tools pay off.
  • Standalone game modes rather than framework-heavy roleplay scripts (most frameworks and examples are Lua).

Comparisons between the three languages: Lua vs C# vs JavaScript.

Frequently asked questions

Can I write FiveM scripts in C#?

Yes. Use the CitizenFX .NET templates to create client and server projects that build to DLLs.

How do I create a FiveM C# project?

dotnet new -i CitizenFX.Templates, then dotnet new cfx-resource in a new folder.

How do I call natives from C#?

Add using static CitizenFX.Core.Native.API; and call them by name, for example PlayerPedId().

Do ESX and QBCore support C#?

They are Lua frameworks; you can call their exports from C#, but most examples and resources are Lua.

Need this built, not just explained?

Ten years of FiveM work, from Lua to NUI

Custom resources, React NUI, ESX / QBCore / Qbox integration, OneSync performance audits and security reviews — plus the websites and SEO around your server brand.

Related guides