101 lines
		
	
	
		
			No EOL
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			No EOL
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| QBCore = exports['qb-core']:GetCoreObject()
 | |
| 
 | |
| Player = nil
 | |
| local npcHandle = nil
 | |
| local isNPCSpawned = false
 | |
| 
 | |
| Citizen.CreateThread(function()
 | |
|     while Player == nil do
 | |
|         Player = exports['qb-core']:GetPlayerData()
 | |
|         Wait(0)
 | |
|     end
 | |
| end)
 | |
| 
 | |
| -- Funktion zum Spawnen des NPCs
 | |
| function SpawnGuardNPC(npc)
 | |
|     -- Ped Model laden
 | |
|     RequestModel(npc.model)
 | |
|     local timeout = 0
 | |
|     while not HasModelLoaded(npc.model) and timeout < 100 do
 | |
|         timeout = timeout + 1
 | |
|         Wait(100)
 | |
|     end
 | |
| 
 | |
|     if not HasModelLoaded(npc.model) then
 | |
|         return
 | |
|     end
 | |
| 
 | |
|     -- NPC erstellen
 | |
|     npcHandle = CreatePed(4, npc.model, npc.spawn.x, npc.spawn.y, npc.spawn.z, npc.spawn.w, false, true)
 | |
|     if not DoesEntityExist(npcHandle) then
 | |
|         return
 | |
|     end
 | |
| 
 | |
|     -- NPC Eigenschaften setzen
 | |
|     SetEntityAsMissionEntity(npcHandle, true, true)
 | |
|     SetBlockingOfNonTemporaryEvents(npcHandle, true)
 | |
|     SetPedDiesWhenInjured(npcHandle, false)
 | |
|     SetPedCanPlayAmbientAnims(npcHandle, true)
 | |
|     SetPedCanRagdollFromPlayerImpact(npcHandle, false)
 | |
|     SetEntityInvincible(npcHandle, true)
 | |
|     FreezeEntityPosition(npcHandle, true)
 | |
|     
 | |
|     -- Optional: Animation für den NPC
 | |
|     TaskStartScenarioInPlace(npcHandle, "WORLD_HUMAN_GUARD_STAND", 0, true)
 | |
|     
 | |
|     isNPCSpawned = true
 | |
| end
 | |
| 
 | |
| -- Funktion zum Entfernen des NPCs
 | |
| function RemoveGuardNPC()
 | |
|     if DoesEntityExist(npcHandle) then
 | |
|         DeleteEntity(npcHandle)
 | |
|         isNPCSpawned = false
 | |
|     end
 | |
| end
 | |
| 
 | |
| -- Hauptthread zum Überprüfen der Spieler-Position
 | |
| CreateThread(function()
 | |
|     while true do
 | |
|         local playerPed = PlayerPedId()
 | |
|         local playerCoords = GetEntityCoords(playerPed)
 | |
| 
 | |
|         for k, v in pairs(Config.Zonen) do
 | |
|             local dist = #(playerCoords - vector3(v.NPC.spawn.x, v.NPC.spawn.y, v.NPC.spawn.z))
 | |
|             local spawnDistance = v.NPC.distance
 | |
|         
 | |
|             if dist < spawnDistance and not isNPCSpawned then
 | |
|                 SpawnGuardNPC(v.NPC)
 | |
|                 Wait(300)
 | |
|                 AddTargetOptions(v)
 | |
|             elseif dist > spawnDistance and isNPCSpawned then
 | |
|                 exports['qb-target']:RemoveTargetEntity(npcHandle)
 | |
|                 RemoveGuardNPC()
 | |
|             end
 | |
|         end
 | |
|         
 | |
|         Wait(0) -- Überprüfung jede Sekunde
 | |
|     end
 | |
| end)
 | |
| 
 | |
| function AddTargetOptions(zone)
 | |
|     exports['qb-target']:AddTargetEntity(npcHandle, {
 | |
|         options = {
 | |
|             {
 | |
|                 type = "client",
 | |
|                 event = "mh_garage:storeVehicle",
 | |
|                 icon = "fas fa-parking",
 | |
|                 label = "Fahrzeug einparken",
 | |
|                 args = zone
 | |
|             },
 | |
|             {
 | |
|                 type = "client",
 | |
|                 event = "mh_garage:retrieveVehicle",
 | |
|                 icon = "fas fa-car",
 | |
|                 label = "Fahrzeug ausparken",
 | |
|                 args = zone
 | |
|             }
 | |
|         },
 | |
|         distance = 2.5
 | |
|     })
 | |
| end | 
