playerInjuries = {}
function OnPlayerTakeDamage(ply, dmginfo)
if ply:IsPlayer() and dmginfo:IsDamageType(DMG_BULLET) then
local hitgroup = ply:LastHitGroup()
local injuryType = ""
local injuryDescription = ""
if hitgroup == HITGROUP_LEFTARM then
local randomNum = RandomNumber(1, 100)
if randomNum >= 75 then
injuryType = injuries["Arm Links"][1]
elseif randomNum <= 75 and randomNum >= 85 then
injuryType = injuries["Arm Links"][2]
elseif randomNum <= 85 and randomNum >= 95 then
injuryType = injuries["Arm Links"][3]
else
injuryType = injuries["Arm Links"][4]
end
elseif hitgroup == HITGROUP_RIGHTARM then
local randomNum = RandomNumber(1, 100)
if randomNum >= 75 then
injuryType = injuries["Arm Rechts"][1]
elseif randomNum <= 75 and randomNum >= 85 then
injuryType = injuries["Arm Rechts"][2]
elseif randomNum <= 85 and randomNum >= 95 then
injuryType = injuries["Arm Rechts"][3]
else
injuryType = injuries["Arm Rechts"][4]
end
elseif hitgroup == HITGROUP_LEFTLEG then
local randomNum = RandomNumber(1, 100)
if randomNum >= 75 then
injuryType = injuries["Bein Links"][1]
elseif randomNum <= 75 and randomNum >= 85 then
injuryType = injuries["Bein Links"][2]
elseif randomNum <= 85 and randomNum >= 95 then
injuryType = injuries["Bein Links"][3]
else
injuryType = injuries["Bein Links"][4]
end
elseif hitgroup == HITGROUP_RIGHTLEG then
local randomNum = RandomNumber(1, 100)
if randomNum >= 75 then
injuryType = injuries["Bein Rechts"][1]
elseif randomNum <= 75 and randomNum >= 85 then
injuryType = injuries["Bein Rechts"][2]
elseif randomNum <= 85 and randomNum >= 95 then
injuryType = injuries["Bein Rechts"][3]
else
injuryType = injuries["Bein Rechts"][4]
end
elseif hitgroup == HITGROUP_CHEST or hitgroup == HITGROUP_GENERIC then
elseif hitgroup == HITGROUP_HEAD then
end
if injuryType ~= "" then
if not playerInjuries[ply] then
playerInjuries[ply] = {}
end
table.insert(playerInjuries[ply], injuryType)
test(ply, injuryType)
end
end
end
function test(ply, injuryType)
ply:ChatPrint(injuryType)
ply:ChatPrint("Spielerverletzungen: " .. table.concat(playerInjuries[ply], ", "))
end
outside the OnPlayerTakeDamage function the player Injuries or players Injuries[ply] array is nil and I don't understand why or how to fix this.
Everything except the array works.If it's helpful. The code is for the game Garry's mod
The array playerInjuries[ply] should have a table for each player that contains all injuries of the respective player.
Before worrying about
playerInjuries[ply], make sure thatplayerInjuriesisn'tnilintest()by printing it. I suspectplayerInjuriesis nevernilin your program, though you may have some code you didn't share that sets it tonil.Make sure
test()is getting called at all, and only by the one spot you shared.Once you made sure
playerInjuriesis nevernilinsidetest, you can addfor k, v in pairs(playerInjuries) do print(k, v) endto print the keys and values in it.