Getting "attempt to index local ply (a nil value)" error for some reason

Hello!

I ran into another issue the other day where I kept getting this error saying “attempt to index local ply (a nil value)”. I see no reason for it to be saying that, everything should be working. Here is the snippet of where it’s coming from:




function getAllMoney(ply,cmd,args)
    if ply:IsSuperAdmin() then
    getGlobalMoneys()
    getInflationRate()
    PrintMessage(HUD_PRINTCONSOLE,"-------------------------------")
    PrintMessage(HUD_PRINTCONSOLE,"The amount of money that all players on this server have is: " .. globalMoney)
    PrintMessage(HUD_PRINTCONSOLE,"The previous supply was: " .. prevGlobal)
    PrintMessage(HUD_PRINTCONSOLE,"The inflation rate is: " .. _inflationRate)
    PrintMessage(HUD_PRINTCONSOLE,"This will be used for the Dynamic Economy addon.")
    PrintMessage(HUD_PRINTCONSOLE,"-------------------------------")
        for k,v in pairs(player.GetAll()) do
        updateMoney()
        end
     else
    PrintMessage(HUD_PRINTCONSOLE,"Sorry, but you're not an admin.")    
    end
end
hook.Add( "Initialize", "get_all_money", getAllMoney )
concommand.Add("rp_getallmoney", getAllMoney)


Here is the error:




[ERROR] addons/dynamiceconomy/lua/autorun/server/dynecon.lua:30: attempt to index local 'ply' (a nil value)
  1. fn - addons/dynamiceconomy/lua/autorun/server/dynecon.lua:30
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:109



The function runs on server startup which you can see by the Initialize hook. Would appreciate any help. Thank you!

The “Initialize” hook does not contain a “ply” argument, see here:

GM:Initialize

When you use the concommand however, it should work?

Yes, the concommand does work. How would I go upon doing this on startup then?

[editline]1st January 2017[/editline]

I could have 2 functions. One for initializing and then one as a command, but there might be another way that I don’t know of.

Check if ply is valid before checking if they’re SuperAdmin.

[editline]2nd January 2017[/editline]

Oh and rather than putting else at the bottom of everything, return if the check fails at the top. That way the script will still run if ply isn’t valid. (You’d have to change your SuperAdmin check to check if they’re not SuperAdmin)