local time = 30 --假設設定是 30 秒
RegisterKeyMapping('test2', '中文說明', 'keyboard', 'Y')
RegisterCommand('test2', function()
if time > 30 then
print('test1') --看你要觸發什麼事件
time = 0
else
timer = 30 - time
ESX.ShowNotification('還需要'..timer..'秒後才能用')
end
end)
Citizen.CreateThread(function()
while time >= 0 do
Citizen.Wait(1000)
if time >= 0 and time <= 30 then
time = time + 1
end
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if time >= 0 and time <= 30 then
timer = 30 - time
drawText(0.93, 0.7, '冷卻等待 '..timer..' 秒', 0.3)
end
end
end)
function drawText(x, y, text, scale)
SetTextFont(0)
SetTextScale(scale, scale)
SetTextColour(255, 255, 255, 215)
SetTextDropShadow(0, 0, 0, 0, 0)
SetTextOutline()
SetTextEntry("STRING")
AddTextComponentString(text)
DrawText(x, y)
local factor = (string.len(text)) / 300
DrawRect(x+0.0325,y+0.0125, 0.015+ factor, 0.03, 0, 0, 0, 255)
end
local isDead = false
AddEventHandler('esx:onPlayerDeath', function(data)
isDead = true
end)
AddEventHandler('playerSpawned', function(spawn)
isDead = false
end)
RegisterKeyMapping('死亡翻身指令', '死亡翻身', 'keyboard', 'U')
RegisterCommand('死亡翻身指令', function()
if isDead then
ClearPedTasksImmediately(GetPlayerPed(-1))
end
end)
local AREA_CENTER = vec3(0.0, 0.0, 0.0) -- 替換為實際的區域中心坐標
local AREA_RADIUS = 50.0 -- 區域範圍
-- 定義應該開啟警燈的車輛生成名稱列表
local EMERGENCY_VEHICLES = {
"police",
"police2",
"police3",
"policet",
"fbi",
"fbi2",
"ambulance",
"firetruk",
-- 在這裡添加更多的車輛生成名稱
}
-- 將列表轉換為一個查找表,以提高效率
local EMERGENCY_VEHICLES_HASH = {}
for _, model in ipairs(EMERGENCY_VEHICLES) do
EMERGENCY_VEHICLES_HASH[GetHashKey(model)] = true
end
-- 使用 memoization 來優化 isEmergencyVehicle 函數
local emergencyVehicleCache = {}
local function isEmergencyVehicle(vehicle)
local model = GetEntityModel(vehicle)
if emergencyVehicleCache[model] == nil then
emergencyVehicleCache[model] = EMERGENCY_VEHICLES_HASH[model] or false
end
return emergencyVehicleCache[model]
end
-- 創建一個單一的 point 實例
local areaPoint
local function createAreaPoint()
if not areaPoint then
areaPoint = lib.points.new({
coords = AREA_CENTER,
distance = AREA_RADIUS,
onEnter = function(self)
local veh = cache.vehicle
if veh and isEmergencyVehicle(veh) and not IsVehicleSirenOn(veh) then
SetVehicleSiren(veh, true)
end
end,
nearby = function(self)
local veh = cache.vehicle
if veh and isEmergencyVehicle(veh) and not IsVehicleSirenOn(veh) then
SetVehicleSiren(veh, true)
end
end
})
end
end
local function removeAreaPoint()
if areaPoint then
areaPoint:remove()
areaPoint = nil
end
end
lib.onCache('vehicle', function(value)
if value and isEmergencyVehicle(value) then
createAreaPoint()
else
removeAreaPoint()
end
end)