How to spawn Peds, fix them and make them invincible?

Hey,
I was wondering about how to spawn peds, freeze them so they dont move and make them invincible.
I tried some Scripts from other Discussions like this:

local firstspawn = 0
local peds = { { -1317.06, -390.76, 36.44} }
local thisPed
local pedCoords = {}
local storedPeds = {}
AddEventHandler('playerSpawned', function(spawn)
	if firstspawn == 0 then
		for i=1, 10, 1 do
			pedCoords = peds[i]
			thisPed = CreatePed(26, 0xF0EC56E2, pedCoords[1], pedCoords[2], pedCoords[3], 0.0, true, true)
			table.insert(storedPeds, {ped = thisPed})
			SetEntityInvincible(thisPed, true)
		end
		firstspawn = 1
	end
end)

But it dont work.

Thanks for help :slight_smile:

Use this:

local NPC = { x = 83.78, y = -855.38, z = 30.77, rotation = 200, NetworkSync = true}
Citizen.CreateThread(function()
  modelHash = GetHashKey("NPC_SPAWN_NAME")
  RequestModel(modelHash)
  while not HasModelLoaded(modelHash) do
       Wait(1)
  end
  createNPCShelby() 
end)
function createNPC()
	created_ped = CreatePed(0, modelHash , NPC.x,NPC.y,NPC.z - 1, NPC.rotation, NPC.NetworkSync)
	FreezeEntityPosition(created_ped, true)
	SetEntityInvincible(created_ped, true)
	SetBlockingOfNonTemporaryEvents(created_ped, true)
	TaskStartScenarioInPlace(created_ped, "WORLD_HUMAN_COP_IDLES", 0, true)
end
2 Likes

Thank you, you helped me a lot :slight_smile:

1 Like

Ehh, i still have a problem. The script dont start because "client.lua:8: attempt to call a nil value (global ‘createNPCShelby’)

How can i fix it?

Picture:

Sorry, my error, take:

local NPC = { x = 83.78, y = -855.38, z = 30.77, rotation = 200, NetworkSync = true}
Citizen.CreateThread(function()
  modelHash = GetHashKey("NPC_SPAWN_NAME")
  RequestModel(modelHash)
  while not HasModelLoaded(modelHash) do
       Wait(1)
  end
  createNPC() 
end)
function createNPC()
	created_ped = CreatePed(0, modelHash , NPC.x,NPC.y,NPC.z - 1, NPC.rotation, NPC.NetworkSync)
	FreezeEntityPosition(created_ped, true)
	SetEntityInvincible(created_ped, true)
	SetBlockingOfNonTemporaryEvents(created_ped, true)
	TaskStartScenarioInPlace(created_ped, "WORLD_HUMAN_COP_IDLES", 0, true)
end
1 Like

Ok, thanks it works but how can i manage to add more npc’s with different models and locations?

MMMMM, its very izi, only copy that code and change de local NPC, the name of the function

Ok, thanks! :slight_smile:

@Kevin_ho
I’ve been looking for something like this to spawn some peds in specific places. Fist of all, thanks for this script. I am quite noob with scripts and managing resources so I have a little doubt. With this script I can spawn a ped in specific “scenarios”. I have looking for a scenario list but I can’t find any for dancing. I have found lots of dancing animations but no scenarios. So, How can I modify the script for npcs making animation instead scenarios?
Thanks in advance.

Mmmmm i think you can use the TaskPlayAnim to play a animation list, here you have a list of animations https://alexguirre.github.io/animations-list/

Thank you! I’ll try this weekend and will tell you how it was :slight_smile:

What do you mean by “change de local NPC, the name of the function” I don’t understand

Hey! I am having issues creating peds standing next to each other. It will spawn in the first one, but not the second one.