Detecting various general events such as death, entering vehicles, weapon shots

So this is one of the main questions I have before starting with my project.
In SA-MP, the server handles everything with the client internally and there are callbacks that are automatically called (OnPlayerDeath, OnPlayerWeaponShot, …) which make life easier.
If I understood things correctly here, I have to manually do those things by constantly checking for the corresponding event every xx milliseconds? Is this how things are also done internally in SA-MP?
Also I have seen some scripts here that looked far from being optimized, like for example checking if a player entered a vehicle in many events, which costs ressources. Instead, would it not be better to manually make a big event called OnPlayerEnterVehicle and doing multiple checks for various things you want done/checked when a player enters a vehicle?
Also, is it better to trust the client and make an OnPlayerEnterVehicle equivalent for each client, then have the server listen for any input, or is it better to have the server loop through all players and constantly check them?
Those are fundamental design choices that I want to make before starting with anything major

1 Like

To address part of your question, there are game events you can listen for:

https://docs.fivem.net/docs/scripting-reference/events/list/gameEventTriggered/
https://docs.fivem.net/docs/game-references/game-events/

1 Like

Hi @Battlezone,

I also came from SA-MP (back a few years ago now) and your question(s) were pretty much what I was wanting answers to as well.

The way FiveM works overall is a lot different to SA-MP. A few points to remember here are:

  • Unlike SA-MP, FiveM doesn’t have anywhere near as many native game events
  • FiveM utilises a client-server relationship
  • Because of the above, client and server scripts are kept separately

So to answer your questions…

In summary yes, however as @ChristopherM mentioned, a lot of the basic things are covered through game events which you can utilise - but remember, not everything is covered in game events so there are still going to be events that you’ll have to manually listen for.

Although I don’t know the official answer to this, I would assume internally they did the same thing but just covered it up to make things a lot simpler to understand and work with (remember, SA-MP didn’t utilise client-server relationships like FiveM does).

You’re correct, anyone who duplicates the exact same function in multiple resources simply does not know how to code performance-efficient scripts. The better way of achieving something like that, as you correctly assumed, would be to create a ‘base’ resource inside of which you’d create an export named OnPlayerEnterVehicle and then you’d have the ability to call that export in any other resource on your server.

A general rule of thumb that I’d suggest for you to follow is only keep the important/logical stuff on the server and allow the client to do the rest. In your example of “OnPlayerEnterVehicle”, you have the choice of either using the client-only native IsPedInAnyVehicle or the client and server native GetVehiclePedIsIn (and check if its -1 or not). Both work a little different but can be utilised for your purpose - you will get the hang of it with time and practice.

Click here to see all available natives
Click here to see all available game events

In summary, I would highly recommend you to research more about client-server relationships and the code structure that compliments such a system. If there was one general advice I wish I had gotten earlier when I started, it would’ve been that.

2 Likes