How to create a pure Javascript resource?

I’ve been slamming my face against the wall of the Javascript scripting / natives documentation all day. I just can’t figure out / find what I need for server / client files and how to get them to communicate.

Can someone show me / find an example of a basic Javascript script (with no or minimal Lua and not for a UI)? Something analogous to I type in “/hat” and the command PED::SET_PED_PROP_INDEX(PLAYER::PLAYER_PED_ID(), 0, 1, 1, 0) runs, but using the Javascript bindings.

Perhaps you haven’t seen this yet http://docs.fivem.net/scripting/javascript/

Other than that, calling natives is the same as in Lua or C#

Do you have a working example you can show me? Cause I’ve already read though all of that, and it’s not helpful.

It doesn’t say if those are client or serverside calls even though the on(…) and onNet(…) methods specifically reference the source as client or server. It talks about window being for the global scope but fails to mention you have to use it, so on(“playerSpawn”, callback) fails, but window.on(“playerSpawn”, callback) works.

Yeah, calling natives supposedly works the same (I haven’t directly confirmed in my attempts) but first you have to go find a buried .d.ts file to confirm the “pretty” name exists instead of the Nx offset, why aren’t the JS Natives and API online? And speaking of the JS API, it isn’t in the file claimed, go ahead and search your natives_universal.d.ts for “on” or “onNet”.

Here’s a simple question, if I type in “/hat” how do I get my local client.js to respond to that?

Edit: And christ is the CitizenFX.log unhelpful for debugging JS scripts. It doesn’t even tell you what line the error is on.

Alright, after rereading some of the Lua examples, I’ve got it minimally working.

In my client.js I have window.RegisterCommand(‘hat’, callback, false); and have a window.emit(‘addChatSuggestion’, ‘hat’, ‘Get a hat’); after it to add the /hat to the suggested commands like /say.

This is pure client, so I still have no idea how JS client-server communication is supposed to work. Does it require registering the events, sending nuis?

Oh and I found window.RegisterCommand in the natives_universal.d.ts and on / onNet (not sure about emit) in the index.d.ts.

It says on the document linked.

To create a client event listener use on(
To create a server event listener use onNet(
To talk to a client script use emit(
To talk to a server script use emitNet(

You should also be able to call Lua / C# events using the same method.

I unfortunately do not, since I haven’t attempted to write a script in JS (yet). Just wanted to point you in the right direction if you didn’t see the docs entry.

It would be great to get JS tutorials on the forum to create a basic resource just like there are for C# and Lua.

Good luck.

There’s actually already some public releases:

No need for explicit window. prefix, either. There’s still no direct wrapper for TRIGGER_CLIENT_EVENT_INTERNAL, so you’ll have to directly call this with a msgpack encoder or just write server-side code using events in Lua/C#.

Which suggests to me that triggering a server event within a server script requires using emitNet() and onNet() even though the network shouldn’t be involved.

Thanks for the example. I think I didn’t understand what was needed for my test server to reflect changes to my resource qnd coincidentally came up with the “window.” explanation.

dont know if this is stupid or not but my question is can start a discord bot as a fivem server resource? I have tried jeva’s discord bot but it has fivem command as it takes data from fivem server. but i am asking like a discord modmail bot or discord music bot.

You won’t want that integrated inside FXServer. Just use a normal modmailbot