How to spawn NPCs

i’m just trying to spawn some peds. I’m using this function client side

Citizen.CreateThread(function()
	RequestModel(GetHashKey( "a_c_cow"))
	while not HasModelLoaded(GetHashKey("a_c_cow")) do
		Citizen.Wait( 1 )
	end
		for k,v in pairs(cfg.positions)do
			entity[k] = CreatePed(4, "a_c_cow", v[1], v[2], v[3]-1, 0.0, true, true)
			FreezeEntityPosition(entity[k], true)
		end
end)

But this is happening: https://media.discordapp.net/attachments/436658091618992158/569296950269313034/unknown.png

Well if you spawn them networked on every client then every client will create his own cow resulting in having 10 cows if 10 players are online

Either spawn them non-networked or choose one person to spawn them serverside or the host

2 Likes

So, can you help me on how to do this? i’ve searched others scripts that do the same thing… but they use the same way as me

Citizen.CreateThread(function()
    modelHash = GetHashKey("HASH HERE")
    RequestModel(modelHash)
    while not HasModelLoaded(modelHash) do
       	Wait(1)
    end
    createNPC() 
end)

function createNPC()
	created_ped = CreatePed(5, modelHash , caravana.x,caravana.y,caravana.z - 1, caravana.rotation, caravana.NetworkSync, caravana.NetworkSync)
	FreezeEntityPosition(created_ped, true)
	SetEntityInvincible(created_ped, true)
	SetBlockingOfNonTemporaryEvents(created_ped, true)
	TaskStartScenarioInPlace(created_ped, "WORLD_HUMAN_DRINKING", 0, true)
end

Where it`s say: caravana.x, caravana.y, caravana.z put your coords and if you do not want an animation just remove the WORD HUMAN DRINKING

1 Like

Both should be true,true?

local spawned = false
Citizen.CreateThread(function()
	RequestModel(GetHashKey( "a_c_cow"))
	while not HasModelLoaded(GetHashKey("a_c_cow")) do
		Citizen.Wait( 1 )
	end
		for k,v in pairs(cfg.positions)do
            if(not spawned)then
			entity[k] = CreatePed(4, "a_c_cow", v[1], v[2], v[3]-1, 0.0, true, true)
			FreezeEntityPosition(entity[k], true)
            if not NetworkGetEntityIsNetworked(entity[k]) then
				NetworkRegisterEntityAsNetworked(entity[k])
			end
            spawned = true
		end
end)

Can’t edit for while sorry i’ve forget an end…

local spawned = false
Citizen.CreateThread(function()
	RequestModel(GetHashKey( "a_c_cow"))
	while not HasModelLoaded(GetHashKey("a_c_cow")) do
		Citizen.Wait( 1 )
	end
		for k,v in pairs(cfg.positions)do
            if(not spawned)then
			entity[k] = CreatePed(4, "a_c_cow", v[1], v[2], v[3]-1, 0.0, true, true)
			FreezeEntityPosition(entity[k], true)
            if not NetworkGetEntityIsNetworked(entity[k]) then
				NetworkRegisterEntityAsNetworked(entity[k])
			end
            spawned = true
		   end
          end
end)

You sure this will work?

I can’t be sure because i don’t have your whole script and can’t test it :face_with_thermometer:

Okay, i’ll try this. Thanks

1 Like

when you get this problem are you the only player connected to your server?

No, like @TheIndra said the problem is happening cause every client is spawning your own entity

Can you share your full client script?

Just set networked bool to false, or only let one client spawn the cows

@TheIndra you say this?

CreatePed(4, "a_c_cow", v[1], v[2], v[3]-1, 0.0, false,false)

Ye something like that, but this means every client has his own cow, but if they aren’t really moving that shouldn’t be a problem.

Yes, they are freezed, i’ll test this. Thank you :slight_smile:

Can maybe help you…

1 Like

afirmative