[Solved] Howto create peds tutorial request

I would like to ask if someone could make a tutorial on how to create a ped on certain locations.

i tried following below “server.lua

Citizen.CreateThread(function()
-- Create Peds for all the stores
 function pedLoad (name)
   CreatePed(1706635382, -46.370, -1758.160, 29.421)
 end
 addEventHandler ("onResourceStart", getResourceRootElement(), pedLoad)
end)

and in my “client.lua

storePed = createPed(1706635382, 46.370, 1758.160, 29.005) 
function cancelPedDamage()
	cancelEvent() 
end
addEventHandler("onClientPedDamage", storePed, cancelPedDamage)

However this fails, so i hope someone can help me out.

1 Like

Something like this should work in your client.lua:

local firstspawn = 0
local peds = { { -46.370, -1758.160, 29.421}, {other}, {locations}, {here} }
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, 1706635382, pedCoords[1], pedCoords[2], pedCoords[3], 0.0, true, true)
			table.insert(storedPeds, {ped = thisPed})
			SetEntityInvincible(thisPed, true)
		end
		firstspawn = 1
	end
end)

I haven’t had a chance to test it, but it should create 10 peds in different locations. You must add more locations in peds or lower the amount it loops.
There is an easier way but I think that it’s a good idea to keep track of the peds you have created, in case you want to delete or access them later.

As for your code, you have the eventHandlers at the bottom and below the end so that it probably one reason for it not working.

3 Likes

@Cosmo
I have a little script that spawns peds on the new nightclub, but for some reason its spawnning peds per player, for example if there’re 2 players online it’ll spawn 2 peds any way to fix this?

Thanks for yout time

local djs = {
{type=4, hash=-508849828, x=-1602.77, y=-3012.490, z=-77.800, a=273.100},
– Bahamamas
{type=4, hash=-86969715, x=-1381.730, y=-614.330, z=31.500, a=141.220},
}

Citizen.CreateThread(function()
– Load Solomun
RequestModel(-508849828)
while not HasModelLoaded(-508849828) do
Wait(1)
end
– Spawn DJs
for _, item in pairs(djs) do
dj = CreatePed(item.type, item.hash, item.x, item.y, item.z, item.a, true, true)
SetBlockingOfNonTemporaryEvents(dj, true)
SetPedDiesWhenInjured(dj, false)
SetPedCanPlayAmbientAnims(dj, true)
SetPedCanRagdollFromPlayerImpact(dj, false)
SetEntityInvincible(dj, true)
–FreezeEntityPosition(dj, true)
TaskPlayAnim(dj,“anim@amb@nightclub@djs@solomun@”,“sol_idle_left_b_sol”, 8.0, 0.0, -1, 1, 0, 0, 0, 0)
end
end)

2 Likes

Sup, my last name is CARNERO, so we maybe related or something (¿)

The solution to that is in the 7th comma on CreatePed(), it is “IsNetwork”, this is TRUE or FALSE. True will spawn a NPC for every player on the server, put that on false.

1 Like

Ahahah maybe :thinking:

I managed to get it working with that yeah, but thanks for your time!

1 Like

Did you solve the problem that NPC are not syncing with all players in scene?, when you put isNetwork to true, every duped npc is sync with every player in the area.

1 Like

Yap i synced all peds for all players is working fine, i even add peds on the disco just like gta online, dj’s, bouncers, dancer everything xD

1 Like

Can you help me with that? i’m tring to make a laboratory zone with guarding NPC. Let me know if you can help me because I’m still getting dual NPC with isNetwork true, and false it will attack each client individual. I’m using npclife script for this, i’m an id1ot on .lua, Thanks!

-- Switch Network FiveM Server Content (https://switchnetwork.co)
-- NPC Life Script: Add NPCs to set co-ordinates with many variables.

-------------------------------------------------------------------------------------------------
---------------------------------- DON'T EDIT THESES LINES --------------------------------------
-------------------------------------------------------------------------------------------------

local generalLoaded = false
local PlayingAnim = false

-------------------------------------------------------------------------------------------------
----------------------------------- YOU CAN EDIT THIS LINES -------------------------------------
-------------------------------------------------------------------------------------------------
RequestModel( GetHashKey( "S_M_Y_Dealer_01" ) )
while ( not HasModelLoaded( GetHashKey( "S_M_Y_Dealer_01" ) ) ) do
    Citizen.Wait( 1 )
end

local ShopClerk = {
  -- ID: id of NPC | name: Name of Blip | BlipID: Icone of Blip | VoiceName: NPC Talk When near it | Ambiance: Ambiance of Shop | Weapon: Hash of Weapon | modelHash: Model | X: Position x | Y: Position Y | Z: Position Z | heading: Where Npc look
	{id = 1, name = "Dealer", BlipID = 52, VoiceName = "SHOP_BANTER_FRANKLIN", Ambiance = "AMMUCITY", Weapon = 0x1D073A89, modelHash = "S_M_Y_Dealer_01", x = -1227.4739990234, y = -1234.9270019531, z = 7.0508131980896, heading = 31.0},
	{id = 2, name = "MissionRowPD", BlipID = 52, VoiceName = "GENERIC_HOWS_IT_GOING", Ambiance = "AMMUCITY", Weapon = 0x1D073A89, modelHash = "S_M_Y_Cop_01", x = 440.83297729492, y = -978.85131835938, z = 30.689601898193, heading = 184.0},
	{id = 3, name = "PDArmory", BlipID = 52, VoiceName = "GENERIC_HOWS_IT_GOING", Ambiance = "ALIENS", Weapon = 0x2BE6766B, modelHash = "a_m_m_og_boss_01", x = -65.478691101074, y = 6239.8149414063, z = 31.091032028198, heading = 50.0},

-- Spawn NPC
Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)
	
	if (not generalLoaded) then
	  
	  for i=1, #ShopClerk do
        RequestModel(GetHashKey(ShopClerk[i].modelHash))
        while not HasModelLoaded(GetHashKey(ShopClerk[i].modelHash)) do
          Wait(1)
        end
		  Wait(4)
        ShopClerk[i].id = CreatePed(1, ShopClerk[i].modelHash, ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, ShopClerk[i].heading, true, false)
        SetPedFleeAttributes(ShopClerk[i].id, 0, 0)
		SetAmbientVoiceName(ShopClerk[i].id, ShopClerk[i].Ambiance)
		SetPedDropsWeaponsWhenDead(ShopClerk[i].id, false)
		SetPedDiesWhenInjured(ShopClerk[i].id, false)
        GiveWeaponToPed(ShopClerk[i].id, ShopClerk[i].Weapon, 2800, false, true)
        SetPedAccuracy(ShopClerk[i].id, 50)
        SetPedCombatMovement(ShopClerk[i].id, 2)
        SetPedSeeingRange(ShopClerk[i].id, 6.0)
	    SetPedHearingRange(ShopClerk[i].id, 5.0)
        SetPedCombatAttributes(ShopClerk[i].id, 46, true)
        SetPedRelationshipGroupHash(ShopClerk[i].id, 0x6F0783F5)
        SetRelationshipBetweenGroups(5,0x6F0783F5, GetHashKey("PLAYER"))
        SetRelationshipBetweenGroups(5,GetHashKey("PLAYER"), 0x6F0783F5)
        SetEntityAsMissionEntity(ShopClerk[i].id,true,true)

      end
      generalLoaded = true
		
    end
	
  end
end)

-------------------------------------------------------------------------------------------------
---------------------------------- DON'T EDIT THESES LINES --------------------------------------
-------------------------------------------------------------------------------------------------

-- Action when player Near NPC (or not)
Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)
		RequestAnimDict("random@shop_gunstore")
		while (not HasAnimDictLoaded("random@shop_gunstore")) do 
			Citizen.Wait(0) 
		end
		
		for i=1, #ShopClerk do
			distance = GetDistanceBetweenCoords(ShopClerk[i].x, ShopClerk[i].y, ShopClerk[i].z, GetEntityCoords(GetPlayerPed(-1)))
			if distance < 5 and PlayingAnim ~= true then
				TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_greeting", 1.0, -1.0, 4000, 0, 1, true, true, true)
				PlayAmbientSpeech1(ShopClerk[i].id, "DYING_MOAN", "SPEECH_PARAMS_FORCE", 1)
				PlayingAnim = true
				SetPedAsEnemy(ShopClerk[i].id, true)
				Citizen.Wait(4000)
				if PlayingAnim == true then
					TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
					Citizen.Wait(40000)
				end
			else
				--don't touch this
				--TaskPlayAnim(ShopClerk[i].id,"random@shop_gunstore","_idle", 1.0, -1.0, -1, 0, 1, true, true, true)
			end
			if distance > 5.5 and distance < 6 then
				PlayingAnim = false
			end


		end
  end
end)

I’ve tried with and without native SetEntityAsMissionEntity and i dont see a difference.

1 Like

This’s what i have and its working:

local djs = {
	{type=4, hash=-508849828, x=-1602.910, y=-3012.700, z=-78.800, a=280.910},
	-- Bahamamas
	{type=4, hash=-86969715, x=-1381.730, y=-614.330, z=30.500, a=141.220},
}

Citizen.CreateThread(function()
  -- Load Solomun
	RequestModel(-508849828)
	while not HasModelLoaded(-508849828) do
		Wait(1)
	end
  -- Load Dix
	RequestModel(-86969715)
	while not HasModelLoaded(-86969715) do
		Wait(1)
	end

  -- Load the animation for Solomun
	RequestAnimDict("anim@amb@nightclub@djs@solomun@")
	while not HasAnimDictLoaded("anim@amb@nightclub@djs@solomun@") do
		Wait(1)
	end

-- #### Spawners #### --
  -- Spawn DJs
	for _, item in pairs(djs) do
		dj =  CreatePed(item.type, item.hash, item.x, item.y, item.z, item.a, false, true)
		SetBlockingOfNonTemporaryEvents(dj, true)
		SetPedDiesWhenInjured(dj, false)
		SetPedCanPlayAmbientAnims(dj, true)
		SetPedCanRagdollFromPlayerImpact(dj, false)
		SetEntityInvincible(dj, true)
		FreezeEntityPosition(dj, true)
		TaskPlayAnim(dj,"anim@amb@nightclub@djs@solomun@","sol_idle_left_b_sol", 8.0, 0.0, -1, 1, 0, 0, 0, 0)
	end
end)
1 Like

where did you get this script