Cannot trigger client function from server script

Hello there.

I want to trigger a client event with a parameter in the server script, but every time I do this I get this error:

[script:CopsAndRobber] SCRIPT ERROR in reference call: System.ArgumentException: method arguments are incompatible
[script:CopsAndRobber] > void CopsAndRobbers.Server.ServerMain.OnResourceStart(string resourceName)+(int source, List<object> args, string raw) => { } (@CopsAndRobbers/CopsAndRobbers.Server.net.dll:0)

client:

public Class1()
{
    EventHandlers["crStart"] += new Action<Player>(SetBlipOfRob);
}

private void SetBlipOfRob(Player robber)
{
    TriggerEvent("chat:addMessage", new
    {
        args = new[] { "crStart" }
    });
    Debug.WriteLine(robber.Character.GetOffsetPosition(new Vector3()).ToString());
}

server:

RegisterCommand("crStart", new Action<int, List<object>, string>((source, args, raw) =>
{
    if (robber != null) 
    {
        TriggerClientEvent("chat:addMessage", new
        {
            args = new[] { "[CopsAndRobbers]", "Start!" }
        });
        TriggerClientEvent("crStart", robber);
    }
}), false);

Am I missing something?
In the docs and forum I could find nothing that would help me.

You cannot send objects over the network.
In this case you can send the player.NetworkId to the server
like

public Class1()
{
    EventHandlers["crStart"] += new Action<int>(SetBlipOfRob);
}

private void SetBlipOfRob(int robberNetId)
{
    var robber = Players[robberNetId];
    TriggerEvent("chat:addMessage", new
    {
        args = new[] { "crStart" }
    });
    Debug.WriteLine(robber.Character.GetOffsetPosition(new Vector3()).ToString());
}

And

 TriggerClientEvent("crStart", robber.NetworkId);

In case for the future if you want to send custom objects you can serialize them to json and back.

1 Like

Thank you for your answer. I will try it when I got time

@Dutchkiller2000 is correct, but you can also further simplify your code.

Server:

[Command("crStart")]
private void OnCrStartCommand([FromSource] Player source) => TriggerClientEvent("crStart", int.Parse(source.Handle));

Client:

[EventHandler("crStart")]
private void OnCrStart(int serverId)
{
    Player robber = Players[serverId];

    if (robber is null)
    {
        return;
    }

    TriggerEvent("chat:addMessage", new
    {
        args = new[] { "[CopsAndRobbers]", "Start!" }
    });

    Debug.WriteLine(robber.Character.GetOffsetPosition(new Vector3()).ToString());
}

Written directly on the forum, so may contain typos.

2 Likes

thank you both for the solutions.
I don’t get the error now.

Unfortunately I can’t use C# 9 yet :smiley:
so it wont work for me now

But now I can’t call the client event.
I don’t know why or I can’t find the issue

server:

public class ServerMain : BaseScript
    {
        Player robber;
        List<Player> copsList;

        public ServerMain()
        {
            EventHandlers["onResourceStart"] += new Action<string>(OnResourceStart);
        }

        private void OnResourceStart(string resourceName)
        {
            if (GetCurrentResourceName() != resourceName) return;

            [...]

            RegisterCommand("crStart", new Action<int, List<object>, string>((source, args, raw) =>
            {
                if (robber != null) 
                {
                    TriggerClientEvent("setBlipOfRob", robber.Name);
                }
            }), false);
        }
    }

client:

public class Class1 : BaseScript
    {
        public Class1()
        {
            EventHandlers["setBlipOfRob"] += new Action<string>(SetBlipOfRob);
        }

        private void SetBlipOfRob(string robber)
        {
            Player rob = Players[robber];
            TriggerEvent("chat:addMessage", new
            {
                args = new[] { "crStart" }
            });
            Debug.WriteLine(rob.Character.GetOffsetPosition(new Vector3()).ToString());
        }
    }

this should be like in the docs for C# listening and triggering events

the fxmanifest.lua is not causing this problem

fx_version 'cerulean'
game 'gta5'

server_script 'CopsAndRobbers.Server.net.dll'
client_script 'CopsAndRobbers.CLient.net.dll'

I could resolve my problem.
I first created this script in lua and registered the same event with the same name.
After stoping the old resource it worked.