[How to] Create peds,vehicles,objects at long distance and place them on ground correctly.(Without falling trough the ground and/or despawn)o

Hi i present you a way to spawn anythings(peds,vehicles,objects etc)without falling trough the ground and/or despawn at very long distance and hight amount of entitys.

I’ve set some examples commented but fully usable.

local entitys = {} --entitys table
Citizen.CreateThread(function()			 

while true do
Wait(0)		

local player = GetPlayerPed(-1)
local pos = GetEntityCoords(player,1)
local ground 

if #entitys < 15 then--repeat until 15 entitys are spawned

RequestModel("STRATUM")
while not HasModelLoaded("STRATUM") or not HasCollisionForModelLoaded("STRATUM") do --for each types of entity
Wait(1)
end			    

--RequestModel("A_F_M_BODYBUILD_01")
--while not HasModelLoaded("A_F_M_BODYBUILD_01") or not HasCollisionForModelLoaded("A_F_M_BODYBUILD_01") do
--Wait(1)
--end	

--RequestModel("prop_barbell_100kg")
--while not HasModelLoaded("prop_drug_package") or not HasCollisionForModelLoaded("prop_barbell_100kg") do
--Wait(1)
--end					

posX = pos.x+math.random(-1000,1000)--radius example
posY = pos.y+math.random(-1000,1000)--radius example
Z = pos.z+999.0

ground,posZ = GetGroundZFor_3dCoord(posX+.0,posY+.0,Z,1)--set Z pos as on ground

if(ground) then--if ground find

entity = CreateVehicle("STRATUM",posX,posY,posZ,0.0, true, true)--vehicle example
--entity = CreatePed(4,"A_F_M_BODYBUILD_01",posX,posY,posZ,0.0, true, true)--peds example
--entity = CreateObject(GetHashKey("prop_barbell_100kg"),posX,posY,posZ,true, false, true)--object example
SetEntityAsMissionEntity(entity, true, true)--for it to don't be delete if too far from players
table.insert(entitys,entity)--insert entity in tables

local blip = AddBlipForEntity(entity)--add blip for entity
SetBlipSprite(blip,66)
SetBlipColour(blip,46)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("Spawned entity")
EndTextCommandSetBlipName(blip)
--end		              			   
end
end	

 --this part avoid entity to spawn in water
 for i, entity in pairs(entitys) do
     if IsEntityInWater(entity) then--detect if entity is in water.If in water the entity is deleted and the loop restart until the 15 entitys are well on ground
	    local model = GetEntityModel(entity)
	    SetEntityAsNoLongerNeeded(entity)
		SetModelAsNoLongerNeeded(model)
	    DeleteEntity(entity)
	    table.remove(entitys,i)	
	 end
 end
 ------------------------------------------
end
end)

If any questions or need some informations feel free to ask.

Edit:Tested with pickups and work fine.Useful for battle royal or survival gamemodes.

4 Likes

is saved in the database?

Can I add items to the players, such as a chair or a table, so they can put on the floor? Example: Buy a chair in the store and put it in front of your house.

how to leave this code in a loop that always has 15 entities spawned, in the case of a zombie gamemode, always have 15 zombies alive (zombie alive is funny)

Google Translator, sorry!

Hi, you need to add this:

 for i, entity in pairs(entitys) do
     if IsPedDeadOrDying(entity, 1) then--detect if entity is dead.If dead the entity is deleted and the loop restart until entitys == 15
	    local model = GetEntityModel(entity)
	    SetEntityAsNoLongerNeeded(entity)
		SetModelAsNoLongerNeeded(model)
	    DeleteEntity(entity)
	    table.remove(entitys,i)	--delete the entity from the entitys table
	 end
 end

after the “for” loop that check if in water for example.

1 Like

I’ll try, thx!!!

for them to clear the distance, I comment the line SetEntityAsMissionEntity (entity, true, true)

I’m not sure to understand but the distance is updated each frame so it will be always different.

this line that makes peds persistent? SetEntityAsMissionEntity (entity, true, true)
Sorry, I’m using Google Translator

for them to erase the distant peds, I comment the line SetEntityAsMissionEntity (entity, true, true)

Yes it is. But if you need to comment it, be carefull because if you set a long spawning distance the peds will be removed and your loop will never stop.

Thanks, it’s exactly the code I was looking for some time ago.

I’m testing your script now and I noticed that all the peds are gone from time to time, as if you were doing a restart of the script itself

I’ve already figured out how to fix it.

Can you explain the fix ?

I deleted this: HasCollisionForModelLoaded
When I leaned on the ped, it disappeared.

ok, thank you it’s usefull info.

Is there some way to do this for weapons?

1 Like

@mountcheese
Yes, you can use it with weapons pickups.

for me when i do any spawn object script it wont work if im not in the range of the place it wants to spawn. how do i fix this? otherwise the game crashes. neither when you run it via client or server it doesnt work. please help me get a fix.

U need to be rendering the zone, otherwise the native doesnt work, the fix for ur problem is wait until ur near of the zone, then spawn.

Tbh i don’t don’t know why, it seems that something has changed for a while now. In my case it only happend when i share object id, entity id etc between clients. It broked some of my scripts and i never tried to fix this. Better ask in the resource development section,there is probably more efficient way to achieve this. Rendering the zone could probability help, it wasn’t efficient before.

how do you render a chunk via script, without teleporting a player?

By rendering the camera at the spawn coords iirc.But not sure.
About the crash this is not related to the used method i’ve tested it recently with AI spawning and everything work flawlessly btw. As i said before my only issue is when i share net id through the server, now the entity only get detected when the no host players come close to them. @olliebolie