Hello,
I do not use " if(hitinfo.Initiator:ToPlayer()) then" without making mistakes.
Code :
Error :Code:function PLUGIN:OnEntityDeath(entity, hitinfo) if(entity:GetComponent("BaseNPC")) then if(hitinfo.Initiator:ToPlayer()) then player = hitinfo.Initiator:ToPlayer() userdata = API:GetUserDataFromPlayer(player) animal = entity:LookupShortPrefabName() if animal == "bear.prefab" and Settings.Enabled.Bear == "true" then userdata:Deposit(tonumber(Settings.Amount.Bear)) if Settings.Enabled.Messages == "true" then rust.SendChatMessage(player, BuildOutput(Settings.Messages.Kill, {"{prefix}", "{amount}", "{entity}"}, {Settings.Messages.Prefix, Settings.Amount.Bear, Settings.Name.Bear})) end elseif
Line: 56 => if(hitinfo.Initiator:ToPlayer()) thenCode:6:23 PM [Error] Failed to call hook 'OnEntityDeath' on plugin 'testhunt v0.0.3' File: testhunt.lua Line: 56 attempt to index field 'Initiator' (a nil value): at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in <filename unknown>:0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in <filename unknown>:0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args) [0x00000] in <filename unknown>:0 at NLua.LuaFunction.Call (System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Ext.Lua.Plugins.LuaPlugin.OnCallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0
Would know how to use her without creating error?
Solved Using hitinfo.Initiator:ToPlayer() (Lua)
Discussion in 'Rust Development' started by Loup-des-Neiges, Oct 25, 2015.
-
Wulf Community Admin
The Initiator would be who killed the player, which may not always exist. You should be doing a nil/null check on it before trying to convert to a player.
If you are wanting to see who actually died, you'd want entity:ToPlayer(). -
Code:
if hitinfo.Initiator:ToPlayer() ~= nil then
-
Wulf Community Admin
Remove :ToPlayer()
if not hitInfo.Initiator then return end -
No error , but it no longer works x)
Code:function PLUGIN:OnEntityDeath(entity, hitinfo) if(entity:GetComponent("BaseNPC")) then if not hitInfo.Initiator then return end player = hitinfo.Initiator:ToPlayer() userdata = API:GetUserDataFromPlayer(player) animal = entity:LookupShortPrefabName() print("Joueur : "..player.." Data : "..userdata.." Animal : "..animal)
-
Wulf Community Admin
-
Sorry, not well understood.
My goal is to know who killed animal.
1- if(entity:GetComponent("BaseNPC")) then => I verifi it's an animal.
2- if(hitinfo.Initiator:ToPlayer()) then => I verifi that this is a player who killed the animal.
then
player = hitinfo.Initiator:ToPlayer() => player = player who killed the animal.
[DOUBLEPOST=1445880786,1445802568][/DOUBLEPOST]Nobody knows why this makes mistakes and how to avoid it? -
Wulf Community Admin