From aca2e75b3b28a2afaf37e71293f5ff56b25e65c1 Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Sun, 27 Jul 2025 22:47:15 +0200 Subject: [PATCH] Update client.lua --- resources/[standalone]/nordi_tdm/client.lua | 108 ++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/resources/[standalone]/nordi_tdm/client.lua b/resources/[standalone]/nordi_tdm/client.lua index e3bc6d0b0..f06467bc0 100644 --- a/resources/[standalone]/nordi_tdm/client.lua +++ b/resources/[standalone]/nordi_tdm/client.lua @@ -822,3 +822,111 @@ CreateThread(function() end end) +-- Function to spawn NPCs for all fields +function spawnFieldNPCs() + for fieldId, fieldData in pairs(Config.gameFields) do + if fieldData.lobby and fieldData.lobby.npc then + local npcData = fieldData.lobby.npc + local model = GetHashKey(npcData.model) + + -- Request the model + RequestModel(model) + while not HasModelLoaded(model) do + Wait(10) + end + + -- Create the NPC + local npc = CreatePed(4, model, npcData.coords.x, npcData.coords.y, npcData.coords.z, npcData.coords.w, false, true) + SetEntityAsMissionEntity(npc, true, true) + SetBlockingOfNonTemporaryEvents(npc, true) + FreezeEntityPosition(npc, true) + SetEntityInvincible(npc, true) + + -- Add to spawned NPCs table + table.insert(spawnedNPCs, npc) + + -- Add target interaction + exports['qb-target']:AddTargetEntity(npc, { + options = { + { + type = "client", + event = "tdm:openMenu", + icon = "fas fa-gamepad", + label = "TeamDeathmatch Menu", + fieldId = fieldId + } + }, + distance = 2.0 + }) + + debugPrint("Spawned NPC for field: " .. fieldId) + end + end +end + +-- Register event handler for NPC interaction +RegisterNetEvent('tdm:openMenu', function(data) + if data.fieldId then + openMainMenu(data.fieldId) + end +end) + +-- Spawn NPCs when resource starts +CreateThread(function() + Wait(1000) -- Wait for everything to load + spawnFieldNPCs() +end) + +-- Function to create blips for all TDM lobbies +function createTDMBlips() + for fieldId, fieldData in pairs(Config.gameFields) do + if fieldData.lobby and fieldData.lobby.pos then + local blip = AddBlipForCoord(fieldData.lobby.pos.x, fieldData.lobby.pos.y, fieldData.lobby.pos.z) + SetBlipSprite(blip, 156) -- You can change this to any appropriate sprite + SetBlipDisplay(blip, 4) + SetBlipScale(blip, 0.8) + SetBlipColour(blip, 3) + SetBlipAsShortRange(blip, true) + BeginTextCommandSetBlipName("STRING") + AddTextComponentString("TDM: " .. fieldData.name) + EndTextCommandSetBlipName(blip) + + table.insert(tdmBlips, blip) + debugPrint("Created blip for TDM field: " .. fieldId) + end + end +end + +-- Call this function when the resource starts +CreateThread(function() + Wait(2000) -- Wait for everything to load + createTDMBlips() +end) + +-- Cleanup function +function cleanupResources() + -- Remove NPCs + for _, npc in ipairs(spawnedNPCs) do + if DoesEntityExist(npc) then + DeleteEntity(npc) + end + end + spawnedNPCs = {} + + -- Remove blips + for _, blip in ipairs(tdmBlips) do + if DoesBlipExist(blip) then + RemoveBlip(blip) + end + end + tdmBlips = {} + + debugPrint("Cleaned up all NPCs and blips") +end + +-- Register cleanup when resource stops +AddEventHandler('onResourceStop', function(resourceName) + if GetCurrentResourceName() == resourceName then + cleanupResources() + end +end)