Skip to content

Commit

Permalink
fix(server/commands): giving keys
Browse files Browse the repository at this point in the history
* 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.

* feat: Added new config setting

Added a new config option "distanceToHandKeys = 3" so players can adjust the range at which to hand keys over.
  • Loading branch information
zozomanx authored Dec 19, 2024
1 parent ed40e3a commit 02666e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/server.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {
runClearCronMinutes = 5,
distanceToHandKeys = 3,
---@type table<WeaponTypeGroup, number>
carjackChance = { -- Probability of successful carjacking based on weapon used
[WeaponTypeGroup.MELEE] = 0.0,
Expand Down
16 changes: 11 additions & 5 deletions server/commands.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -81,4 +87,4 @@ lib.addCommand(locale('addcom.addkeys'), {
}, function (source, args)
local playerId = args[locale('addcom.addkeys_id')]
transferKeys(source, playerId, false)
end)
end)

0 comments on commit 02666e9

Please sign in to comment.