Main/resources/[carscripts]/mh_garage/client/main.lua

98 lines
2.7 KiB
Lua
Raw Normal View History

2025-06-08 12:19:35 +02:00
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
2025-06-08 13:12:49 +02:00
function SpawnGuardNPC(npc)
2025-06-08 12:19:35 +02:00
-- Ped Model laden
RequestModel(npc.model)
2025-06-08 13:44:19 +02:00
local timeout = 0
2025-06-08 13:45:42 +02:00
while not HasModelLoaded(npc.model) and timeout < 100 do
2025-06-08 13:44:19 +02:00
timeout = timeout + 1
Wait(100)
2025-06-08 12:19:35 +02:00
end
2025-06-08 13:45:42 +02:00
if not HasModelLoaded(npc.model) then
2025-06-08 13:44:19 +02:00
return
end
2025-06-08 12:19:35 +02:00
-- NPC erstellen
2025-06-08 13:52:14 +02:00
npcHandle = CreatePed(4, npc.model, npc.spawn.x, npc.spawn.y, npc.spawn.z, npc.spawn.w, false, true)
2025-06-08 13:44:19 +02:00
if not DoesEntityExist(npcHandle) then
return
end
2025-06-08 12:19:35 +02:00
-- NPC Eigenschaften setzen
SetEntityAsMissionEntity(npcHandle, true, true)
SetBlockingOfNonTemporaryEvents(npcHandle, true)
SetPedDiesWhenInjured(npcHandle, false)
SetPedCanPlayAmbientAnims(npcHandle, true)
SetPedCanRagdollFromPlayerImpact(npcHandle, false)
SetEntityInvincible(npcHandle, true)
2025-06-08 13:53:03 +02:00
FreezeEntityPosition(npcHandle, true)
2025-06-08 12:19:35 +02:00
-- Optional: Animation für den NPC
TaskStartScenarioInPlace(npcHandle, "WORLD_HUMAN_GUARD_STAND", 0, true)
isNPCSpawned = true
end
-- Funktion zum Entfernen des NPCs
2025-06-08 13:12:49 +02:00
function RemoveGuardNPC()
2025-06-08 12:19:35 +02:00
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
2025-06-08 12:30:15 +02:00
local dist = #(playerCoords - vector3(v.NPC.spawn.x, v.NPC.spawn.y, v.NPC.spawn.z))
2025-06-08 12:19:35 +02:00
local spawnDistance = v.NPC.distance
if dist < spawnDistance and not isNPCSpawned then
2025-06-08 14:04:18 +02:00
AddTargetOptions()
2025-06-08 12:35:06 +02:00
SpawnGuardNPC(v.NPC)
2025-06-08 12:19:35 +02:00
elseif dist > spawnDistance and isNPCSpawned then
2025-06-08 14:04:18 +02:00
exports['qb-target']:RemoveTargetEntity(npcHandle)
2025-06-08 12:19:35 +02:00
RemoveGuardNPC()
end
end
2025-06-08 14:04:18 +02:00
Wait(0) -- Überprüfung jede Sekunde
2025-06-08 12:19:35 +02:00
end
2025-06-08 14:04:18 +02:00
end)
function AddTargetOptions()
exports['qb-target']:AddTargetEntity(npcHandle, {
options = {
{
type = "client",
event = "garage:storeVehicle",
icon = "fas fa-parking",
2025-06-08 15:19:18 +02:00
label = "Fahrzeug einparken"
2025-06-08 14:04:18 +02:00
},
{
type = "client",
event = "garage:retrieveVehicle",
icon = "fas fa-car",
2025-06-08 15:19:18 +02:00
label = "Fahrzeug ausparken"
2025-06-08 14:04:18 +02:00
}
},
distance = 2.5
})
end