Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve an error when giving keys without ID #158

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading