local QBCore = exports['qb-core']:GetCoreObject() local spawnedNPCs = {} -- NPC Spawn Function (gleich wie oben) local function SpawnNPC(npcId, npcData) local model = GetHashKey(npcData.model) RequestModel(model) while not HasModelLoaded(model) do Wait(1) end local npc = CreatePed(4, model, npcData.coords.x, npcData.coords.y, npcData.coords.z - 1.0, npcData.coords.w, false, true) SetEntityHeading(npc, npcData.coords.w) FreezeEntityPosition(npc, true) SetEntityInvincible(npc, true) SetBlockingOfNonTemporaryEvents(npc, true) exports['qb-target']:AddTargetEntity(npc, { options = { { type = 'client', event = 'npc-dialog:client:openDialog', icon = npcData.targetIcon, label = npcData.targetLabel, npcId = npcId } }, distance = 2.0 }) spawnedNPCs[npcId] = npc SetModelAsNoLongerNeeded(model) end -- Alle NPCs spawnen CreateThread(function() for npcId, npcData in pairs(Config.NPCs) do SpawnNPC(npcId, npcData) end end) -- Dialog öffnen - Zeigt zuerst NPC-Text, dann Optionen RegisterNetEvent('npc-dialog:client:openDialog', function(data) local npcId = data.npcId local npcData = Config.NPCs[npcId] if not npcData then return end -- Zuerst NPC-Text als Alert anzeigen lib.alertDialog({ header = npcData.dialog.title, content = npcData.dialog.description, centered = true, cancel = false, labels = { confirm = 'Antworten' } }):next(function(confirmed) if confirmed then -- Dann Dialog-Optionen anzeigen ShowDialogOptions(npcData, npcId) end end) end) -- Dialog-Optionen anzeigen function ShowDialogOptions(npcData, npcId) local options = {} for i, option in ipairs(npcData.dialog.options) do table.insert(options, { title = option.title, description = option.description, icon = option.icon, onSelect = function() if option.info then lib.alertDialog({ header = option.title, content = option.info, centered = true, cancel = true, labels = { cancel = 'Zurück' } }) elseif option.response then ShowResponse(option.response, npcData, npcId) end end }) end table.insert(options, { title = "🚪 Verlassen", description = "Dialog beenden", icon = 'times' }) lib.registerContext({ id = 'npc_options_' .. npcId, title = "Antworten:", options = options }) lib.showContext('npc_options_' .. npcId) end -- Response anzeigen function ShowResponse(response, npcData, npcId) lib.alertDialog({ header = response.title, content = response.description, centered = true, cancel = false, labels = { confirm = 'Weiter' } }):next(function(confirmed) if confirmed and response.options then local options = {} for i, option in ipairs(response.options) do table.insert(options, { title = option.title, description = option.description, icon = option.icon, onSelect = function() if option.info then lib.alertDialog({ header = option.title, content = option.info, centered = true, cancel = true, labels = { cancel = 'Schließen' } }) elseif option.response then ShowResponse(option.response, npcData, npcId) end end }) end table.insert(options, { title = "← Zurück", description = "Zurück zu den Hauptoptionen", icon = 'arrow-left', onSelect = function() ShowDialogOptions(npcData, npcId) end }) lib.registerContext({ id = 'npc_response_' .. npcId .. '_' .. math.random(1000, 9999), title = "Antworten:", options = options }) lib.showContext('npc_response_' .. npcId .. '_' .. math.random(1000, 9999)) end end) end -- Cleanup AddEventHandler('onResourceStop', function(resourceName) if GetCurrentResourceName() ~= resourceName then return end for _, npc in pairs(spawnedNPCs) do if DoesEntityExist(npc) then DeleteEntity(npc) end end end)