Yakuzashi Posted December 11, 2019 Share Posted December 11, 2019 local function error(inst) inst.components.health:DoDelta(-15) end Code above is located inside custom item's prefab. How to make it work ? I have tried with player/owner instead of inst but it resulted in error anyway. Link to comment Share on other sites More sharing options...
Ultroman Posted December 11, 2019 Share Posted December 11, 2019 (edited) What do you mean "make it work"? You call the function, providing it an instance of an entity as the parameter. local function error(inst) inst.components.health:DoDelta(-15) end local function someOtherFunction(inst) error(inst) end Edited December 11, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Yakuzashi Posted December 11, 2019 Author Share Posted December 11, 2019 You are right, I should precise it. So the error occurs when I try to call function 'error' activated by component: Quote inst:AddComponent("spellcaster") inst.components.spellcaster:SetSpellFn(error) Caster's HP should decrease by 15 points, but instead the error pops up. Quote attempt to index field 'health' (a nil value) Link to comment Share on other sites More sharing options...
Ultroman Posted December 12, 2019 Share Posted December 12, 2019 (edited) Does the inst with the spellcaster component also have a health component? Are those lines maybe placed before the health component is added? If this is a player character, then they should have a health component automatically. If it is on a weapon, then you're trying to remove health from the weapon. In order to get the holder of the weapon, you need to look at its inventoryitem component, and get its "grand owner" (when in a container, like the backpack, the owner is the backpack, but the grand owner is the player), and then see if it has a health component, and if it does, give it damage. local function error(inst) if inst then local invitem = inst.components.inventoryitem if invitem then local owner = invitem:GetGrandOwner() if owner and owner.components.health then owner.components.health:DoDelta(-15) end end end end Edited December 12, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Yakuzashi Posted December 12, 2019 Author Share Posted December 12, 2019 15 hours ago, Ultroman said: Does the inst with the spellcaster component also have a health component? Are those lines maybe placed before the health component is added? If this is a player character, then they should have a health component automatically. If it is on a weapon, then you're trying to remove health from the weapon. In order to get the holder of the weapon, you need to look at its inventoryitem component, and get its "grand owner" (when in a container, like the backpack, the owner is the backpack, but the grand owner is the player), and then see if it has a health component, and if it does, give it damage. local function error(inst) if inst then local invitem = inst.components.inventoryitem if invitem then local owner = invitem:GetGrandOwner() if owner and owner.components.health then owner.components.health:DoDelta(-15) end end end end Thanks, this works just fine. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now