Make cars spawn at coords

Hi. Now im trying to start a zombie server. All im missing are the cars on the street.
Would be ideal if they would just randomly spawn around the player, but having the server spawn them at certain coords is fine as well. Might even be better, as long as it doesn’t spawn it every time a new player joins in but rather does it server side if you know what i mean.

I tried everything from the topic i found in the forums : Custom Car Spawn

Nothing worked. The game wouldn’t even start when i tried Bob’s script which seemed to be the one that worked for others.

Im just hoping its not too hard to pull off. I am slowly trying to learn lua by editing and trying to understand existing stuff. But i can’t figure this out and seems like there’s nothing like that out there either. There is a car spawner script that comes with the zombie mod itself, but it only works once when you get to the server. You get a car or two around you and then nothing else. Works the same way as the zombie spawner itself does, but for some reason cars stop spawning while zombies keep spawning in fine. For comparison, ill include the zombie spawner script and the car spawner script. They both come from RottenV

This is the car spawner ( i reduced the list to make it a bit easier to read)

> -- A list of vehicles that should be spawned
> local spawnableCars =
> {
> 	"Adder",
> }
> 
> -- CODE --
> 
> -- CODE --
> 
> players = {}
> 
> RegisterNetEvent("Z:playerUpdate")
> AddEventHandler("Z:playerUpdate", function(mPlayers)
> 	players = mPlayers
> end)
> 
> RegisterNetEvent("spawnNewVehicle")
> 
> cars = {}
> 
> Citizen.CreateThread(function()
> 	while true do
> 		Wait(1)
> 
> 		if #cars < 20 then
> 			x, y, z = table.unpack(GetEntityCoords(PlayerPedId(), true))
> 
> 			local newVehicleX = x
> 			local NewVehicleY = y
> 			local NewVehicleZ = 0
> 
> 			repeat
> 				Wait(1)
> 				newVehicleX = x + math.random(-1000, 1000)
> 				NewVehicleY = y + math.random(-1000, 1000)
> 				_,NewVehicleZ = GetGroundZFor_3dCoord(newVehicleX+.0,NewVehicleY+.0,z+999.0, 1)
> 			until NewVehicleZ ~= 0
> 
> 			choosenCar = spawnableCars[math.random(1, #spawnableCars)]
> 			RequestModel(choosenCar)
> 			while not HasModelLoaded(choosenCar) or not HasCollisionForModelLoaded(choosenCar) do
> 				Wait(1)
> 			end
> 
> 			car = CreateVehicle(choosenCar, newVehicleX, NewVehicleY, NewVehicleZ, math.random(), true, true)
> 			SetVehicleEngineHealth(car, math.random(400,1000)+0.0)
> 			PlaceObjectOnGroundProperly(car)
> 			if not NetworkGetEntityIsNetworked(car) then
> 				NetworkRegisterEntityAsNetworked(car)
> 			end
> 			TriggerServerEvent("registerNewVehicle", NetworkGetNetworkIdFromEntity(car)) -- these events are dummies, they don't do anything
> 			table.insert(cars, car)
> 		end
> 
> 		for i, car in pairs(cars) do
> 			if not DoesEntityExist(car) or GetEntityHealth(car) == 0 then
> 				SetEntityAsNoLongerNeeded(car)
> 				table.remove(cars, i)
> 				TriggerServerEvent("removeOldVehicle", NetworkGetNetworkIdFromEntity(car))
> 			else
> 				local	playerX, playerY = table.unpack(GetEntityCoords(PlayerPedId(), true))
> 				local	carX, carY = table.unpack(GetEntityCoords(car, false))
> 
> 				if carX < playerX - 1000 or carX > playerX + 1000 or carY < playerY - 1000 or carY > playerY + 1000 then
> 					-- Set car as no longer needed for despawning
> 					SetEntityAsNoLongerNeeded(car)
> 					table.remove(cars, i)
> 					TriggerServerEvent("removeOldVehicle", NetworkGetNetworkIdFromEntity(car))
> 				end
> 			end
> 		end
> 	end
> end)
> 
> --[[ Debug
> Citizen.CreateThread(function()
> 	while true do
> 		Citizen.Wait(0)
> 		for i, car in pairs(cars) do
> 			playerX, playerY, playerZ = table.unpack(GetEntityCoords(PlayerPedId(), true))
> 			carX, carY, carZ = table.unpack(GetEntityCoords(car, false))
> 			--	DrawLine(playerX,playerY, playerZ, carX, carY, carZ, 255.0,0.0,0.0,255.0)
> 		end
> 	end
> end)
> --]]
> 
> RegisterNetEvent("Z:cleanup")
> AddEventHandler("Z:cleanup", function()
> 	for i, car in pairs(cars) do
> 		-- Set car as no longer needed for despawning
> 		Citizen.InvokeNative(0xB736A491E64A32CF, Citizen.PointerValueIntInitialized(car))
> 		TriggerServerEvent("removeOldVehicle", NetworkGetNetworkIdFromEntity(car))
> 		table.remove(cars, i)
> 	end
> end)

And this is the zombie spawner. (reduced the ped list for the same reasons)

-- CONFIG --

local pedModels =
{
	"U_M_Y_Zombie_01"
}
-- CODE --

players = {}

RegisterNetEvent("Z:playerUpdate")
AddEventHandler("Z:playerUpdate", function(mPlayers)
	players = mPlayers
end)

peds = {}

Citizen.CreateThread(function()
	AddRelationshipGroup("zombeez")
	SetRelationshipBetweenGroups(5, GetHashKey("zombeez"), GetHashKey("PLAYER"))
	SetRelationshipBetweenGroups(5, GetHashKey("zombeez"), GetHashKey("bandit"))
	SetRelationshipBetweenGroups(5, GetHashKey("PLAYER"), GetHashKey("zombeez"))

	SetAiMeleeWeaponDamageModifier(2.0)

	while true do
		Wait(1)
		if #peds < 15 then
			x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))

			choosenPed = pedModels[math.random(1, #pedModels)]
			choosenPed = string.upper(choosenPed)
			RequestModel(GetHashKey(choosenPed))
			while not HasModelLoaded(GetHashKey(choosenPed)) or not HasCollisionForModelLoaded(GetHashKey(choosenPed)) do
				Wait(1)
			end

			local newX = x
			local newY = y
			local newZ = z + 999.0

			repeat
				Wait(1)

				newX = x + math.random(-50, 50)
				newY = y + math.random(-50 , 50)
				_,newZ = GetGroundZFor_3dCoord(newX+.0,newY+.0,z, 1)

				for _, player in pairs(players) do
					Wait(1)
					playerX, playerY = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
					if newX > playerX - 35 and newX < playerX + 35 or newY > playerY - 35 and newY < playerY + 35 then
						canSpawn = false
						break
					else
						canSpawn = true
					end
				end
			until canSpawn

			ped = CreatePed(4, GetHashKey(choosenPed), newX, newY, newZ, 0.0, true, true)
			SetPedArmour(ped, 100)
			SetPedAccuracy(ped, 25)
			SetPedSeeingRange(ped, 100.0)
			SetPedHearingRange(ped, 80.0)

			SetPedFleeAttributes(ped, 0, 0)
			SetPedCombatAttributes(ped, 16, 1)
			SetPedCombatAttributes(ped, 17, 0)
			SetPedCombatAttributes(ped, 46, 1)
			SetPedCombatAttributes(ped, 1424, 0)
			SetPedCombatAttributes(ped, 5, 1)
			SetPedCombatRange(ped,2)
			SetPedAlertness(ped,3)
			SetAmbientVoiceName(ped, "ALIENS")
			SetPedEnableWeaponBlocking(ped, true)
			SetPedRelationshipGroupHash(ped, GetHashKey("zombeez"))
			DisablePedPainAudio(ped, true)
			SetPedDiesInWater(ped, false)
			SetPedDiesWhenInjured(ped, false)
			--	PlaceObjectOnGroundProperly(ped)
			SetPedDiesInstantlyInWater(ped,true)
			SetPedIsDrunk(ped, true)
			SetPedConfigFlag(ped,100,1)
			RequestAnimSet("move_m@drunk@verydrunk")
			while not HasAnimSetLoaded("move_m@drunk@verydrunk") do
				Wait(1)
			end
			SetPedMovementClipset(ped, "move_m@drunk@verydrunk", 1.0)
			ApplyPedDamagePack(ped,"BigHitByVehicle", 0.0, 9.0)
			ApplyPedDamagePack(ped,"SCR_Dumpster", 0.0, 9.0)
			ApplyPedDamagePack(ped,"SCR_Torture", 0.0, 9.0)
			StopPedSpeaking(ped,true)

			TaskWanderStandard(ped, 1.0, 10)
			local pspeed = math.random(20,70)
			local pspeed = pspeed/10
			local pspeed = pspeed+0.01
			SetEntityMaxSpeed(ped, 5.0)

			if not NetworkGetEntityIsNetworked(ped) then
				NetworkRegisterEntityAsNetworked(ped)
			end

			table.insert(peds, ped)
		end

		for i, ped in pairs(peds) do
			if DoesEntityExist(ped) == false then
				table.remove(peds, i)
			end
			pedX, pedY, pedZ = table.unpack(GetEntityCoords(ped, true))
			if IsPedDeadOrDying(ped, 1) == 1 then
				-- Set ped as no longer needed for despawning
				local dropChance = math.random(0,100)
				if GetPedSourceOfDeath(ped) == PlayerPedId() then
					if dropChance >= 95 then
						ForceCreateFoodPickupAtCoord(pedX,pedY,pedZ)
					end
					zombiekillsthislife = zombiekillsthislife+1
					zombiekills = zombiekills+1
				end

				Citizen.InvokeNative(0xB736A491E64A32CF, Citizen.PointerValueIntInitialized(ped))
				table.remove(peds, i)
			else
				playerX, playerY = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
				SetPedArmour(ped, 100)
				SetPedAccuracy(ped, 25)
				SetPedSeeingRange(ped, 300.0)
				SetPedHearingRange(ped, 300.0)

				SetPedFleeAttributes(ped, 0, 0)
				SetPedCombatAttributes(ped, 16, 1)
				SetPedCombatAttributes(ped, 17, 0)
				SetPedCombatAttributes(ped, 46, 1)
				SetPedCombatAttributes(ped, 1424, 0)
				SetPedCombatAttributes(ped, 5, 1)
				SetPedCombatRange(ped,2)
				SetAmbientVoiceName(ped, "ALIENS")
				SetPedEnableWeaponBlocking(ped, true)
				SetPedRelationshipGroupHash(ped, GetHashKey("zombeez"))
				DisablePedPainAudio(ped, true)
				SetPedDiesInWater(ped, false)
				SetPedDiesWhenInjured(ped, false)
				if pedX < playerX - 70 or pedX > playerX + 70 or pedY < playerY - 70 or pedY > playerY + 70 then
					-- Set ped as no longer needed for despawning
					local model = GetEntityModel(ped)
					SetEntityAsNoLongerNeeded(ped)
					SetModelAsNoLongerNeeded(model)
					table.remove(peds, i)
				end
			end
		end
	end
end)

RegisterNetEvent("Z:cleanup")
AddEventHandler("Z:cleanup", function()
	for i, ped in pairs(peds) do
		-- Set ped as no longer needed for despawning
		local model = GetEntityModel(ped)
		SetEntityAsNoLongerNeeded(ped)
		SetModelAsNoLongerNeeded(model)

		table.remove(peds, i)
	end
end)

Note again that zombies keep spawning but cars stop. Tried increasing the numbers etc, no luck.
It doesn’t need to be this exact script to get some cars scattered on the map. I’d highly appreciate any kind of help.

Can someone please help with this ? How do i make cars spawn at certain coords once per server restart? Or any kind of spawning would be fine.

I’ve been looking for some similar script since I do a series in the form of a zombie clip for youtube and every time I do a chapter I have to spaw the cars to break them and make the stage and once I leave these disappear. I would like you to find a way for them to stay there but I think it is not possible

I leave one of the chapters to give you an idea

1 Like

bump!

!bump