Initialize Lua array in a function and use it outside. I do not get it

45 views Asked by At
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.

1

There are 1 answers

4
MyNameIsTrez On

outside the OnPlayerTakeDamage function the player Injuries or players Injuries[ply] array is nil

Before worrying about playerInjuries[ply], make sure that playerInjuries isn't nil in test() by printing it. I suspect playerInjuries is never nil in your program, though you may have some code you didn't share that sets it to nil.

Make sure test() is getting called at all, and only by the one spot you shared.

Once you made sure playerInjuries is never nil inside test, you can add for k, v in pairs(playerInjuries) do print(k, v) end to print the keys and values in it.