[Solved] [Javascript] Client can emit server events, but server cannot emit client events

server.js

var clockedIn = {};

RegisterNetEvent('onduty:clockin');
onNet('onduty:clockin', data => {
  clockedIn[data.playerId] = {};
  clockedIn[data.playerId]['rank'] = data.rank;
  clockedIn[data.playerId]['department'] = data.department;
  clockedIn[data.playerId]['category'] = data.category;
  console.log(GetPlayerName(GetPlayerFromIndex(data.playerId)) + ' clocked in | ' + data.rank + ' (' + data.department + ')[' + data.category + ']');
  //console.log(clockedIn);
  emitNet('onduty:update', clockedIn);
});

client.js

RegisterNetEvent('onduty:update');
onNet('onduty:update', data => {
  clockedIn = data;
  if(clockedIn[serverId] !== undefined) {
    onduty = true;
    rank = clockedIn[serverId].rank;
    department = clockedIn[serverId].department;
    category = clockedIn[serverId].category;
  } else {
    onduty = false;
    rank = '';
    department = '';
    category = '';
  }
  console.log('updated');
  createEntityBlips();
});

I have tried every combination of using emitNet, emit, TriggerClientEvent, TriggerEvent, on, onNet, addEventListener. I have tried all combinations of invoking and listening for the event and yet to prevail. I get no errors in console both in-game or server. I have scanned the docs left, right, and center. I have scanned other javascript resources, none of which I have seen use events to connect client and server. I have tried making a lua file that will just pass along the trigger, so it would go from the server.js, pass the data to forward.lua, then TriggerClientEvent in the lua script, no prevail. I’m sure this is something I am doing completely wrong as this is my first large script I am working on.

Another thing I tried was using setImmediate which didn’t work. I remember working on a smaller script and having this same issue but can’t find the code for it.

Any help would be appreciated.

Hi,

It’s a common error, you just forgot to specify a player id in emitNet :wink:

And just like that, script works like a charm. Thank you so much for the quick response