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.
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.
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
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.
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
No matter what I do, I always end up with an error telling me there’s no UI frame.
@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);
}
}
}
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);