From 89e66b27acfa132f67fb4a99deda31bd703204c1 Mon Sep 17 00:00:00 2001 From: zozomanx Date: Thu, 5 Dec 2024 19:22:02 -0500 Subject: [PATCH 1/2] fix: resolve error when giving keys without id When attempting to give keys to another player without an ID an error was given. It had to do with closestDistance being nil. --- server/commands.lua | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/server/commands.lua b/server/commands.lua index 3423dfa..c87bff5 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -1,14 +1,17 @@ +local config = require 'config.server' + ---@param src number ---@return number? local function getClosestPlayer(src) local playerCoords = GetEntityCoords(GetPlayerPed(src)) - local nearbyPlayers = lib.getNearbyPlayers(playerCoords, 3) - local closestPlayer, closestDistance + local nearbyPlayers = lib.getNearbyPlayers(playerCoords, config.distanceToHandKeys) + local closestPlayer + local closestDistance = config.distanceToHandKeys for i = 1, #nearbyPlayers do local nearbyPlayer = nearbyPlayers[i] - if nearbyPlayer.id ~= source then + if nearbyPlayer.id ~= src then local distance = #(nearbyPlayer.coords - playerCoords) - if not distance or distance < closestDistance then + if not distance or distance <= closestDistance then closestPlayer = nearbyPlayer closestDistance = distance end @@ -48,6 +51,9 @@ local function transferKeys(source, target, enforceSrcHasKeys) local closestPlayer = getClosestPlayer(source) if closestPlayer then GiveKeys(closestPlayer, vehicle) + exports.qbx_core:Notify(source, locale('notify.gave_keys')) + else + exports.qbx_core:Notify(source, locale('notify.not_near'), 'error') end end end @@ -81,4 +87,4 @@ lib.addCommand(locale('addcom.addkeys'), { }, function (source, args) local playerId = args[locale('addcom.addkeys_id')] transferKeys(source, playerId, false) -end) \ No newline at end of file +end) From 6c9b81f8090f1e4d1b9e2f56e0ef692a9f297a18 Mon Sep 17 00:00:00 2001 From: zozomanx Date: Thu, 5 Dec 2024 19:24:05 -0500 Subject: [PATCH 2/2] feat: Added new config setting Added a new config option "distanceToHandKeys = 3" so players can adjust the range at which to hand keys over. --- config/server.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/config/server.lua b/config/server.lua index b51d90a..0b705ee 100644 --- a/config/server.lua +++ b/config/server.lua @@ -1,5 +1,6 @@ return { runClearCronMinutes = 5, + distanceToHandKeys = 3, ---@type table carjackChance = { -- Probability of successful carjacking based on weapon used [WeaponTypeGroup.MELEE] = 0.0,