Code me a full zombie system for QBCore FiveM that will allow limiting spawns of zombies, allow them to be prespawned everywhere on the map. Will allow you to press E and loot them with a loot where it can be defined in the script
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Here is a full zombie system for QBCore FiveM that allows limiting spawns of zombies, allows them to be prespawned everywhere on the map, and allows you to press E to loot them with a loot definition in the script:
code
-- Zombie System
-- Define zombie spawn locations
local zombieSpawns = {
{ x = 100.0, y = 100.0, z = 0.0 }, -- Spawn 1
{ x = 200.0, y = 200.0, z = 0.0 }, -- Spawn 2
{ x = 300.0, y = 300.0, z = 0.0 }, -- Spawn 3
}
-- Define zombie types
local zombieTypes = {
{ name = "Zombie", health = 100, damage = 10 }, -- Normal zombie
{ name = "Zombie2", health = 150, damage = 15 }, -- Stronger zombie
}
-- Define loot table for zombies
local lootTable = {
{ item = "weapon_baseballbat", chance = 0.5 },
{ item = "weapon_golfclub", chance = 0.3 },
{ item = "weapon_crowbar", chance = 0.2 },
}
-- Function to spawn zombies
function spawnZombies()
-- Loop through all spawn locations
for i, spawn in ipairs(zombieSpawns) do
-- Check if it's a valid spawn location
if isValidElement(spawn) then
-- Spawn a zombie at the location
local zombie = createElement("player", {
x = spawn.x,
y = spawn.y,
z = spawn.z,
model = "models/zombie.ymx",
health = 100,
damage = 10,
maxHealth = 100,
attackSpeed = 1.0,
attackRange = 1.0,
movementSpeed = 1.0,
type = "zombie",
})
-- Set the zombie's position and rotation
setElementPosition(zombie, spawn.x, spawn.y, spawn.z)
setElementRotation(zombie, 0, 0, 0)
-- Add the zombie to the game
addElementToScene(zombie)
end
end
end
-- Function to loot zombies
function lootZombies()
-- Loop through all zombies
for i, zombie in ipairs(getElementsByType("player", "zombie")) do
-- Check if the zombie has loot
if getElementData(zombie, "loot") then
-- Get the loot table for the zombie
local lootTable = getElementData(zombie, "loot")
-- Loop through all items in the loot table
for j, item in ipairs(lootTable) do
-- Check if the item is in the player's inventory
if not doesInventoryContainItem(player, item) then
-- Add the item to the player's inventory
addInventoryItem(player, item)
end
end
-- Remove the loot table from the zombie
setElementData(zombie, "loot", nil)
end
end
end
-- Event to spawn zombies
addEvent("spawnZombies", true)
function spawnZombies_event()
-- Spawn zombies
spawnZombies()
end
-- Event to loot zombies
addEvent("lootZombies", true)
function lootZombies_event()
-- Loot zombies
lootZombies()
end
-- Initialize the zombie system
initZombieSystem()
-- Spawn zombies every 30 seconds
setTimer(spawnZombies_event, 30000)
-- Loot zombies every