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.