[Help] Resurrecting Peds

Two problems:
First problem is that you are setting up npcSpawn as a local inside the for-loop. It will only be available in the for-loop.

In order to get npcSpawn to work, you’ll need to define it outside of the for-loop:

local npcSpawn = 0
for k, v in ipairs(Config.Peds) do
  -- do whatever
end

This will not work properly either though, the reason being that you spawn multiple peds, however npcSpawnwill only cover one ped. This means that only one ped could ever be respawned. To fix this you’ll want to create a list

npcSpawns = {}

and put all your npcs (or rather the ped ids) into that list.


As I don’t see how you are checking for whether the ped died, you might want to check out CEventNetworkEntityDamage, e.g. here: Some Game Events and how to use them
You can use this in an event handler. It’ll automatically tell you whether the ped died (args[6] in the event). so you can trigger the resurrect-function automatically.


As you have a list named Config.Peds I’d like to propose a different method. For each ped add the value pedId (your npcSpawn) to the list. When you spawn the ped, edit the list to add the proper id (which in your loop would be Config.Peds[k].pedId = npcSpawn).
Now when a ped has died (see above for a probably quick way to do it) you can either ResurrectPed() (which might work with fixes to your code) or if that fails respawn the ped. For that delete the initial ped and just spawn a new one at the old location.


Please note op that while I hope you are satisfied with the answer it is not enough to solve all of your problems. You will most likely encounter issues as soon as more than one person is on the server.
I’d encourage you to first make the script work for yourself first. Getting it to work for multiple people is doable, I’d like to work on these things separately though. Feel free to ask if anything is unclear :slight_smile:

2 Likes