[C#] Call client event on server-side?

Hey,

I’ve been searching on how to do it, but no results so far, i am trying to send a message to players on the server-side, but it always throws errors, so i tried to call an event on the client-side to send the message, but i don’t know how to register/call it.

That’s what i got so far:

Server:

using CitizenFX.Core;
using System;

namespace server
{
    class Main : BaseScript
    {
        public Main()
        {
            EventHandlers["chatMessage"] += new Action<int, string, string>(OnPlayerText);
        }

        private void OnPlayerText(int player, string playerName, string chatMessage)
        {
            TriggerEvent("clientEventTest");            
        }
    }
}

Client:

using System;
using CitizenFX.Core;

namespace client
{
    public class Main : BaseScript
    {
        public Main()
        {
            EventHandlers["clientEventTest"] += new Action<dynamic>(ThisIsATest);
        }

        private void ThisIsATest(dynamic spawn)
        {
            TriggerEvent("chatMessage", "", new[] { 0, 0, 0 }, "Yes this is a chat message.");
        }
    }
}

It should send a message on chat after a player send a message.

The client-side isn’t being called, no errors on console.
Probably it need to be registered somewhere.

Thanks.

Tried TriggerClientEvent? :confused:

There isn’t TriggerClientEvent in C# yet.

There is though? Are you using the right CitizenFX.Core.dll? (the one coming with the server)

Player.TriggerEvent()
Look at the PlayerClass

public class Player
	{
		private string m_handle;

		public string Handle => m_handle;

		internal Player(string sourceString)
		{
			if (sourceString.StartsWith("net:"))
			{
				sourceString = sourceString.Substring(4);
			}

			m_handle = sourceString;
		}

		public string Name => Call<string>(GET_PLAYER_NAME, m_handle);

		public int Ping => Call<int>(GET_PLAYER_PING, m_handle);

		public int LastMsg => Call<int>(GET_PLAYER_LAST_MSG, m_handle);

		public IdentifierCollection Identifiers => new IdentifierCollection(this);

		public string EndPoint => Call<string>(GET_PLAYER_ENDPOINT, m_handle);

		public void Drop(string reason) => Call(DROP_PLAYER, m_handle, reason);

		public void TriggerEvent(string eventName, params object[] args)
		{
			var argsSerialized = MsgPackSerializer.Serialize(args);

			unsafe
			{
				fixed (byte* serialized = &argsSerialized[0])
				{
					Call(TRIGGER_CLIENT_EVENT_INTERNAL, eventName, m_handle, serialized, argsSerialized.Length);
				}
			}
		}
	}

Oh! Sorry! I was using the one from nuget packages.

Just referenced the one that comes with the server now it worked. Thank you!