• DarkRP addMoney is a nil value
    5 replies, posted
I made this string of code and added it into the darkrp modules:[CODE]function GM:OnNPCKilled(victim, killer, weapon) if victim:GetClass() == "npc_zombie" then killer:AddMoney(10) end if victim:GetClass() == "npc_fastzombie" then killer:AddMoney(20) end if victim:GetClass() == "npc_poisonzombie" then killer:AddMoney(100) end end end[/CODE] But everytime i kill any of those npcs it will add the cash and give me this error in console:[CODE][ERROR] gamemodes/darkrp/gamemode/modules/zombie cash/sv_zombiecash.lua:3: attempt to call method 'addMoney' (a nil value) 1. unknown - gamemodes/darkrp/gamemode/modules/zombie cash/sv_zombiecash.lua:3[/CODE] How can I fix this? ------------------------------------------------------------------------ THIS IS MY CODE NOW v [CODE]function GM:OnNPCKilled(victim, killer, weapon) if victim:GetClass() == "npc_headcrab" and killer():IsPlayer() and IsValid(killer) then killer:addMoney(5) end if victim:GetClass() == "npc_zombie" and killer():IsPlayer() and IsValid(killer) then killer:addMoney(15) end if victim:GetClass() == "npc_fastzombie" and killer():IsPlayer() and IsValid(killer) then killer:addMoney(75) end if victim:GetClass() == "npc_headcrabzombie" and killer():IsPlayer() and IsValid(killer) then killer:addMoney(200) end if victim:GetClass() == "npc_poisonheadcrabzombie_elite" and killer():IsPlayer() and IsValid(killer) then killer:addMoney(1000) end end[/CODE] I no longer recieve any errors
You are typing "AddMoney" in your OnNPCKilled functions, it has to be addMoney. Also, use darkrpmodification and don't make your modules in the darkrp gamemode.
You have to check if the killer is a player before giving them money, and the method is addMoney with a lower-case "a". Your error shows that, but your code has AddMoney. Also, use elseif for the class checks.
Shouldn't this be in a hook? And that error doesn't line up with your code. The "A" in "AddMoney" is capitalized in your code but not in the error.
[QUOTE=Stardust342;52465677]Shouldn't this be in a hook? And that error doesn't line up with your code. The "A" in "AddMoney" is capitalized in your code but not in the error.[/QUOTE] It should be the same thing, but what he's doing is overwriting the gamemode's default OnNPCKilled function. He should be using hook.Add instead.
[QUOTE=mib999;52465684]It should be the same thing, but what he's doing is overwriting the gamemode's default OnNPCKilled function. He should be using hook.Add instead.[/QUOTE] So instead of function i should put hook.Add?
Sorry, you need to Log In to post a reply to this thread.