[C#] TriggerServerEvent

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.

Yeah… I’m not sure if its the way to do it, but its how I got it to work :grin:

Did you ever figure out the NUI stuff?

Unfortunately, no. No matter what I do, I always end up with “Resource ‘name’ has no UI frame.”

Oh… are you sending the nui message with C# or Lua? I couldnt figure out the shit with C#, so I just made an event in Lua that I triggered for it.

What exactly is your issue with the no ui frame? When/where do you get that error?

1 Like

I’ve only been trying NUI with Lua, as C# is a rather long way around it. If I remember correctly the error happens when I start the resource. Be default, though, when the resource starts it’s supposed to hide the UI, so it triggers an event that uses the send nui.

I’m not sure, but that might be the issue, because it is sending that message when the player isn’t fully loaded yet. Instead of doing that, in the css, try doing display:none;. That will hide whatever element(s). Then, when you want the stuff to show, just trigger that when the player is in.

@Briglair Do you know of any useful tutorials other than this one?

I dont :confused: