[CLIENT] C# playerSpawned not working properly


#1

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}");
            }
        }
    }
}


#2

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.


#3

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:


#4

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?


#5

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.