While Lua and JavaScript are the most common languages for FiveM development, C# (C Sharp) offers strong typing, compiled performance, and access to the rich .NET ecosystem. This guide covers the basics of setting up and writing C# resources.
To develop in C#, you need:
MyResource.Client or MyResource.Server).You need to reference the FiveM client and server assemblies.
%localappdata%\FiveM\FiveM.app).citizen\clr2\lib\mono\4.5.CitizenFX.Core.dll (Required for both Client and Server)CitizenFX.Core.Client.dll (For Client scripts only)CitizenFX.Core.Server.dll (For Server scripts only)Configure your project to output the compiled .dll files to a folder inside your server’s resources directory.
A C# resource typically has two projects in one solution: one for Client and one for Server.
You still need an fxmanifest.lua to define the resource and load the assemblies.
fx_version 'cerulean'
game 'gta5'
files {
'MyResource.Client.net.dll',
'MyResource.Server.net.dll'
}
client_script 'MyResource.Client.net.dll'
server_script 'MyResource.Server.net.dll'In C#, scripts inherit from BaseScript.
Client/Main.cs)using System;
using System.Threading.Tasks;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;
namespace MyResource.Client
{
public class Main : BaseScript
{
public Main()
{
// Event Handlers
EventHandlers["onClientResourceStart"] += new Action<string>(OnClientResourceStart);
// Register a command
RegisterCommand("hello_csharp", new Action<int, List<object>, string>((source, args, raw) =>
{
TriggerEvent("chat:addMessage", new
{
color = new[] { 255, 0, 0 },
args = new[] { "[C#]", "Hello from C# Client!" }
});
}), false);
}
private void OnClientResourceStart(string resourceName)
{
if (GetCurrentResourceName() != resourceName) return;
Debug.WriteLine($"The resource {resourceName} has started on the client.");
}
}
}Server/Main.cs)using System;
using CitizenFX.Core;
namespace MyResource.Server
{
public class Main : BaseScript
{
public Main()
{
EventHandlers["onResourceStart"] += new Action<string>(OnResourceStart);
}
private void OnResourceStart(string resourceName)
{
if (GetCurrentResourceName() != resourceName) return;
Debug.WriteLine($"The resource {resourceName} has started on the server.");
}
}
}// Client to Server
TriggerServerEvent("myResource:serverEvent", "arg1", 123);
// Server to Client
Player player = Players[source];
player.TriggerEvent("myResource:clientEvent", "data");public Main()
{
EventHandlers["myResource:serverEvent"] += new Action<Player, string, int>(OnServerEvent);
}
private void OnServerEvent([FromSource] Player player, string arg1, int arg2)
{
Debug.WriteLine($"Received from {player.Name}: {arg1}, {arg2}");
}Access FiveM natives via the CitizenFX.Core.Native.API static class.
// Example: Spawning a vehicle
int vehicleHash = GetHashKey("adder");
RequestModel((uint)vehicleHash);
while (!HasModelLoaded((uint)vehicleHash)) await Delay(0);
int vehicle = CreateVehicle((uint)vehicleHash, x, y, z, heading, true, false);fxmanifest.lua.