Knowing whether a ped or vehicle was created by a script

Hello,
I want to know whether a ped or a vehicle was created (and is controlled by) a script / plugin / whatever, or if it’s part of the regular game (spawned by GTA V itself).

Is there a native or a group of natives that could help me accomplish this task?

Thanks in advance.

-------- Update
I randomly found this native: GET_ENTITY_POPULATION_TYPE (link here) but I didn’t test it yet. I’ll edit this post if this solves the issues.

-------- Update
A ped created with CreatePed seems to consistently return POPTYPE_MISSION (7) in GetEntityPopulationType, while normal peds started by the game return 4, 5 and 6. I assume is it then safe to use this native to detect whether a player is spawning scripted peds without the server’s authorization (via a mod).
I didn’t try this native on a vehicle yet.

-------- Update
Works the same with vehicles (returns POPTYPE_MISSION for vehicles spawned with CreateVehicle, while normal vehicles return 4, 5 and 6).
I’ll mark this thread as solved in a few days if I don’t experience any side-effect.

Use server side:

AddEventHandler("entityCreated",  function(entity)
    print("Entity created" .. entity)
end)

And with NetworkGetEntityOwner you can check a player who created specified ped, then use your function as you have posted above, also in your scripts when PED is created you can add SetDecorInt to that ped to mark is as “script created”, and then check client side if it is created by a player and is NOT having that decor then it is obviously created by a mod menu or some other thing.
:mascot:

1 Like

Population types are more accurate and a tad more tricky to assign unintentionally.

Thanks a lot, I didn’t remember there was an entityCreated server event :slight_smile:
I was facing the issue that when I used NetworkGetEntityOwner on a ped/vehicle/prop, it only pointed to the creator when he was near the entity, otherwise it pointed to the nearest player (e.g. the victim being caged or trolled)
Edit: I found out it’s a OneSync-only event, so I won’t be able to use it… Maybe I can detect a new entity in a continuous client-side loop, and report the new ones to the server, which is probably what that event does in OneSync.

So it’s better not to use SetDecorInt right?

It depends, you have to find out a way to mark entity that it might be spawned with a script you created and its ok for that entity to be spawned, otherwise your system will ban players that for example have taken vehicle out of their garage or something like that (if your server have things like that of course)

I ended up creating a list of network-ids of valid entities (that have POPTYPE_MISSION) on the server. This list is only filled by the server when it triggers a client event which asks the client to create an entity, to which the client responds with a server event containing the network ID of the entity it just created.

Ordinary population is ignored.

About other scripts, fortunately I don’t have to worry about it, because I’m writing all from scratch, and this is why I’m doing these things first :slight_smile:

Thanks for helping me, your posts have been very useful in implementing this.