Skip to content

Commit

Permalink
Fixed Fall Damage [ Error : [ script:qbx_medical] SCRIPT ERROR: @qbx_…
Browse files Browse the repository at this point in the history
…medical/client/damage/damage.lua:211: attempt to index a nil value (field '?') [ script:qbx_medical] > checkForDamage (@qbx_medical/client/damage/damage.lua:211) [ script:qbx_medical] > fn (@qbx_medical/client/damage/damage.lua:225)

Error Explanation: The error "attempt to index a nil value" is occurring in the damage system where it tries to access a field of WEAPONS[weaponHash]. 

1 > I added null check for WEAPONS table
2 > Then added proper initialization of WEAPONS
3 > Then added safety checks when accessing weapon data
4 > Ensure core object is loaded before accessing weapons

Now : 

1 > It Handles fall damage gracefully
2 > Won't throw shitty error even if weapon data isn't loaded yet
3 > Provides feedback even for unknown damage types 🙌
  • Loading branch information
subhampro authored Dec 24, 2024
1 parent 3b79725 commit 46a395b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions client/damage/damage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ local config = require 'config.client'
local sharedConfig = require 'config.shared'
local playerArmor = nil
local damageEffectsEnabled = true
local WEAPONS = exports.qbx_core:GetWeapons()
local WEAPONS = {}

-- Initialize weapons after ensuring core is loaded
CreateThread(function()
while not exports.qbx_core do
Wait(100)
end
WEAPONS = exports.qbx_core:GetWeapons()
end)
---Increases severity of an injury
---@param bodyPartKey BodyPartKey
local function upgradeInjury(bodyPartKey)
Expand Down Expand Up @@ -204,11 +211,12 @@ local function checkForDamage()
if isArmorDamaged or isHealthDamaged then
local damageDone = (Hp - health)
local weaponHash = applyDamage(cache.ped, damageDone, isArmorDamaged)
if weaponHash and not WeaponsThatDamagedPlayer[weaponHash] then
-- Add safety check for WEAPONS table and weaponHash ! Its very useful 🙌
if weaponHash and WEAPONS and WEAPONS[weaponHash] and not WeaponsThatDamagedPlayer[weaponHash] then
TriggerEvent('chat:addMessage', {
color = { 255, 0, 0 },
multiline = false,
args = { locale('info.status'), WEAPONS[weaponHash].damagereason }
args = { locale('info.status'), WEAPONS[weaponHash].damagereason or 'Unknown weapon damage' }
})
WeaponsThatDamagedPlayer[weaponHash] = true
end
Expand Down

0 comments on commit 46a395b

Please sign in to comment.