[C#] TriggerServerEvent

Hello, I seem to be having issues triggering any server events. I’m not having any issues with triggering any client events, that all seems to be working well. However, when I try to use TriggerServerEvent, it almost seems as if the code just stops there. Here’s what i’ve got.

client.net.dll:

public void GetPlayerPosition()
{
    Vector3 position = Game.Player.Character.Position;
    TriggerServerEvent("LastDropPosition", position);
}

server.net.dll

public async Task OnTick()
{
    // ... some code ...
    EventHandlers["LastDropPosition"] += new Action<Vector3>(SetLastDropPosition);
    // ... some code ...
}

public void SetLastDropPosition(Vector3 position)
{
    LastDropPosition = position;
}

From what it looks like, the code after TriggerServerEvent doesn’t continue to run afterwards, and the server acts as if it never happened. Any help would be much appreciated.

Resources Used

[C#] TriggerServerEvent & RegisterEvent?
Triggering Events in C#
https://wiki.fivem.net/wiki/TriggerServerEvent

but from the code you send , you tell to do nothing to your server , it just receive the vector3 and that’s , if you tell him nothing he will do nothing. you talk about triggerClientEvent i don’t see any here.

and the mistake you doing is to initiate your EventHandler in the loop OnTick , you have to initiate it in the constructor .

I’ve tried initializing the event handlers in the constructor, the “First Tick” check was my second attempt, seeing as you can’t trigger anything in the constructors. And the eventhandler tells the server to trigger the SetLastDropPosition method passing the Vector3 position variable, which then tells the server to update the “LastDropPosition” variable I have defined. The triggering of client events aren’t what I’m having an issue with.

define the handler in the constructor don’t trigger it in the constructor cause you are telling him to do a new Action every time it’s trigger.

try to receive your vector3 as dynamic on server side , and cast it as vector3 when you receive it.

Let me see if I understand you here:

So in my server.net.dll I have something like this:

public Main()
{
    EventHandlers["UpdateLastDropPosition"] += new Action<dynamic>(SetLastDropPosition);
    EventHandlers["playerDropped"] += new Action<Player, string, CallbackDelegate>(OnPlayerDropped);
}

public SetLastDropPosition(dynamic position)
{
    position = (Vector3)position;
    LastDropPosition = position;
}

public void OnPlayerDropped([FromSource] Player source, string reason)
{
    TriggerClientEvent(source, "PlayerDrop");
}

And in my client.net.dll it looks like this:

public Main()
{
    // ... some code ...
    EventHandlers["PlayerDrop"] += new Action<string>(PlayerDrop);
}

public void GetPlayerPosition()
{
    Vector3 position = Game.Player.Character.Position;
    TriggerServerEvent("UpdateLastDropPosition", position);
}

public void PlayerDrop(string name)
{
    // ... some code ...
    GetPlayerPosition();
}

If I well understood, you are trying to execute something on a dis connected player ? :thinking:

@Izio_Romain Kinda, I’m just trying to grab the position of the player as they disconnect.

You can’t do this, cause the player disconnected. You can’t deal with a dead client ^^
Your best chance is to save the position every second (just like ES do) and then, save the position to the DB onPlayerDrop

Strange. I’ve gotten it to work, though. Granted I used a different strategy.

Which strategy did you use ?

Sorry, that was poorly worded. What I meant by “different strategy” is basically what you said to do. I’d dug up more info that resulted in that. I appreciate the help!

My initial mistake was to believe that the “playerDropped” event gets called just before allowing the client to disconnect.

Now to move on to the issues I’m having with NUI HTML. The lack of detailed documentation and the poorly worded post doesn’t help much when I run into issues, and posting to the forum is usually my last resort.

I wrote the Nui documentation :cry:

2 Likes

Oh my goodness I’m sorry, don’t get me wrong though, it’s been very helpful. For some reason I just can’t get it to work :frowning:
No matter what I do, I always end up with an error telling me there’s no UI frame.

I’ve been going off this tutorial by the way.

If you can create a new topic and well explain the problem, with source code, maybe I and other could help you :slight_smile:

1 Like

As you mentioned to me in the other topic, what issues are you still having with TriggerServerEvent? What type of errors are you getting?

Let me whip up a quick example so that I can show you the errors it seems to toss.

1 Like

@Briglair For the sake of convenience, I’m just going to include the entirety of my code. I have two separate projects referencing to the correct DLLs.

Server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// External Libraries
using CitizenFX.Core;
using CitizenFX.Core.Native;

namespace KCore
{
    public class Server : BaseScript
    {
        public Server()
        {
            EventHandlers["TestServerEvent"] += new Action<Player, Vector3>(ServerEventMethod);

            Tick += OnTick;
        }

        public async Task OnTick()
        {

            await BaseScript.Delay(0);
        }

        public void ServerEventMethod([FromSource] Player source, Vector3 position)
        {
            Debug.WriteLine("[SERVER EVENT] " + source.Name + " | " + position);
        }
    }
}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// External Libraries
using CitizenFX.Core;
using CitizenFX.Core.Native;
using CitizenFX.Core.UI;

namespace KCore
{
    public class Client : BaseScript
    {
        public Client()
        {


            Tick += OnTick;
        }

        public async Task OnTick()
        {

            if (Game.IsControlJustReleased(0, Control.Talk))
            {
                CitizenFX.Core.UI.Screen.ShowNotification("Pre Event Trigger");
                Vector3 position = Game.Player.Character.Position;
                TriggerServerEvent("TestServerEvent", position);
                CitizenFX.Core.UI.Screen.ShowNotification("Post Event Trigger");
            }

            await BaseScript.Delay(0);
        }
    }
}

And last, but certainly not least, my Resource

resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

client_script 'client/kcore.client.net.dll'
server_script 'server/kcore.server.net.dll'

Once ran, I find that the Pre Event notification executes, but once the event tries to trigger, the code stop and I get this error that begins with: “Failed to run a tick for Client: … some error … Number of parameters specified does not match the expected number.”

And if I include the player as a parameter, which I didn’t think was needed, I get an error as such: “Failed to run a tick for Client: System.NullReferenceException: Object reference not set to an instance of an object.”

I had the same exact issue when I tried to send over a Vector3. I don’t know why. I had to send over x y z individually and then receive the x y z on server to get it to work.

So, TriggerServerEvent("TestServerEvent", position.x, position.y, position.z);
then
EventHandlers["TestServerEvent"] += new Action<Player, float, float, float>(ServerEventMethod);

2 Likes

Oh, wow. Talk about an easy fix. Goodness.