[CLIENT] C# playerSpawned not working properly

It only prints “[CLIENT]Hello World! Raimund Tick!!!”, won’t print on spawn event, doesn’t react at all… (e.g.: “[CLIENT]Spawned!”)
P.S. spawnmanager loaded successfully, every other stuff working.
Server:

using System;
using System.Threading.Tasks;
using CitizenFX.Core;
// using static CitizenFX.Core.Native.API;

namespace CataclysmServer
{
    public class CataclysmServer : BaseScript
    {
        private bool firstTick = false;
        public static string Motd = "Cake is a lie ";

        public CataclysmServer()
        {
            Tick += OnTick;
            EventHandlers.Add("cataclysm:playerSpawn", new Action<Player>(OnPlayerSpawned));
            EventHandlers.Add("cataclysm:LogToServer", new Action<Player, string>(OnLogToServer));
        }
        private async Task OnTick()
        {
            if (!firstTick)
            {
                firstTick = true;
                CitizenFX.Core.Debug.Write("[SERVER]Hello World! Raimund Tick!!!!!!!!!!!!!!!!!!!!!!!");
            }

            await Task.FromResult(0);
        }
        private void OnPlayerSpawned([FromSource] Player source)
        {
            CitizenFX.Core.Debug.Write("[SERVER]Hello World! Raimund SPAWNED!!!!!!!!!!!!!!!!!!!!!!!");
            TriggerEvent("chatMessage", "SYSTEM", new[] { 255, 0, 0 }, Motd);
        }
        private void OnLogToServer([FromSource] Player source, string text)
        {
            CitizenFX.Core.Debug.Write(text);
        }
    }
}

Client:

using System;
using System.Threading.Tasks;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;

namespace CataclysmClient
{
    public class CataclysmClient : BaseScript
    {
        public bool hasReceivedMotd = false;
        private bool firstTick = false;

        public CataclysmClient()
        {
            Tick += OnTick;
        }
        private async Task OnTick()
        {
            if (!firstTick)
            {
                EventHandlers.Add("playerSpawned", new Action<Vector3>(PlayerSpawned));
                TriggerServerEvent("cataclysm:LogToServer", "[CLIENT]Hello World! Raimund Tick!!!!!!!!!!!!!!!!!!!!!!!");
                firstTick = true;
            }

            await Task.FromResult(0);
        }
        private void PlayerSpawned([FromSource] Vector3 spawn)
        {
            TriggerServerEvent("cataclysm:LogToServer", "[CLIENT]Spawned!!!!!!!!!!!!!!!!!!!!!!!");
            try
            {
                TriggerServerEvent("cataclysm:playerSpawn");
            }
            catch (Exception ex)
            {
                TriggerServerEvent("cataclysm:LogToServer", $"HandlePlayerSpawned Error: {ex.Message}");
            }
        }
    }
}

Don’t register the event in the OnTick function. but rather in constructor

Moved to #development:scripts, since this seems more like a scripting issue.

No, it doesn’t work! (Tried) I have the latest version of server and client.
LogToServer only works fine, but spawn has issues that i don’t know… :frowning:

There should be no problem registering event handlers in the constructor. I’m not sure if it’d be an issue with the event being triggered vs an issue with it being received. If you create a lua resource to listen for the same event, does that work like intended?

I’ve noticed that exported lua functions or triggered events don’t work with C# code well :frowning: Don’t know how about vice-versa.
So I managed to write my own spawnmanager and baseevents in C#.

P.S. I can’t reproduce because there is no errors by compiling code and adding try/catch.

Please share your baseevents and spawnmanager that was written in C#.
Much Appreciated!

Hey all, Very new to the community modding scene, so please forgive me for ressurrecting a dead topic - I found this topic yesterday - it had no solid conclusion so I decided to start porting the spawnmanager to C#, got to my last function and then I had a moment of clarity.

You can indeed interperate Lua events within C#, they would just come through as something weird like JSON - Like reading encoded strings from the web or a file.

So I got to probing, and what do you know?

Now, Lets talk code;

using System.Dynamic;

public PluginController(){
      EventHandlers["playerSpawned"] += new Action<ExpandoObject>(OnPlayerSpawned);
}

private void OnPlayerSpawned(ExpandoObject spawn){

      Debug.WriteLine($"Spawn String: "+spawn.ToString());
      foreach(KeyValuePair<string, object> kvp in spawn){
          Debug.WriteLine($"Key: "+kvp.Key+" Value: "+kvp.Value);
      }
}

Side note: I have no idea why I’m putting $ infront of my strings, I saw it in the tutorials and just followed suit - I presume its something to do with formatting.

Appologies if this is now common sense - I couldnt find the solution anywhere online.

1 Like