emitNet() and onNet() not working

I am trying to send a netId from my server.js to my client.js so I can add some properties to the entity, however I can’t seem to send it?

// server.js

const modelHash = GetHashKey('ig_thornton')
const Ped = CreatePed(1, modelHash, 24.390533447265625, -1347.23754882812, 29.49705123901367, -90.0, true, true)

console.log('ped created')

while(!DoesEntityExist(Ped)) {
  Wait(0)
}

const netId = NetworkGetNetworkIdFromEntity(Ped)
// console.log('network id:', netId)

emitNet('shop:ped', -1, netId)
// client.js

onNet('shop:ped', (ped) => {
 console.log('[CLIENT] received ped', ped)
});

I don’t get anything in console from inside the onNet(...) function so am I using the wrong one or am I setting it up incorrectly?

This could be setup incorrectly, are you able to share your fxmanifest?

Also - is this the code exact?

If so, you should try…

setImmediate(() => {
const modelHash = GetHashKey('ig_thornton')
const Ped = CreatePed(1, modelHash, 24.390533447265625, -1347.23754882812, 29.49705123901367, -90.0, true, true)

console.log('ped created')

while(!DoesEntityExist(Ped)) {
  Wait(0)
}

const netId = NetworkGetNetworkIdFromEntity(Ped)
// console.log('network id:', netId)

emitNet('shop:ped', -1, netId)
})

Only one other thing I can think of, is sometimes the V8 runtime Wait has been known to cause issues, so you could try something like…


export const Delay = async (ms) => new Promise(res => setTimeout(res, ms));

setImmediate(async () => {
const modelHash = GetHashKey('ig_thornton')
const Ped = CreatePed(1, modelHash, 24.390533447265625, -1347.23754882812, 29.49705123901367, -90.0, true, true)

console.log('ped created')

while(!DoesEntityExist(Ped)) {
  await Delay(0);
}

const netId = NetworkGetNetworkIdFromEntity(Ped)
// console.log('network id:', netId)

emitNet('shop:ped', -1, netId)
})

For the first snippet you sent, nothing changed.

For the second snippet, I got an export error. However, I had a sleep function that does the exact same thing and used that instead of Wait() and it worked, thank you so much! I didn’t know that Wait() causes issues.

Edit: It seems to be a one time thing because I can’t seem to retrieve it from the client’s end.

If anyone knows why, please let me know:


fx_version 'cerulean'
game 'gta5'

author 'Shifts'
description 'Shop'
version '1.0.0'

client_script 'client.js'
server_script 'server.js'

The client.js file does fire and the code reaches the emitNet() line but it doesn’t fire inside the onNet()


const Delay = async (ms) => new Promise((res) => setTimeout(res, ms));

setImmediate(async () => {
    await Delay(100);

    const modelHash = GetHashKey("ig_thornton");
    const Ped = CreatePed(1, modelHash, 24.390533447265625, -1347.23754882812, 29.49705123901367, -90.0, true, true);

    console.log("ped created");

    while (!DoesEntityExist(Ped)) {
        await Delay(0);
    }

    const netId = NetworkGetNetworkIdFromEntity(Ped);
    // console.log('network id:', netId)

    emitNet("shop:ped", -1, netId);
});

And the client remains the same.

The server side executes faster than the client registers the event handler.