AmbiguousMonk Posted September 20, 2018 Share Posted September 20, 2018 I'm attempting to write a function that will loop over each of the body parts of a player and restore them to full health using RestoreToFullHealth(), but it seems that if I loop over them as so: local parts = getPlayer():getBodyDamage():getBodyParts(); for i=0, (getPlayer():getBodyDamage():getBodyParts():size() - 1) do parts[i]:RestoreToFullHealth(); end then I get a "java.lang.RuntimeException: attempted index: RestoreToFullHealth of non-table: null" error and I'm not sure why. Can anyone explain to me why this is happening and/or the appropriate way to accomplish what I'm trying to do? Link to comment Share on other sites More sharing options...
Fenris_Wolf Posted September 21, 2018 Share Posted September 21, 2018 The BodyParts array is defined in the java with a size of 18. There are only 17 BodyParts, thus its probably the last one thats returning null. A simple sanity check should solve your issue. if parts[i] then parts[i]:RestoreToFullHealth() end Link to comment Share on other sites More sharing options...
AmbiguousMonk Posted September 22, 2018 Author Share Posted September 22, 2018 Thank you for your suggestion Fenris_Wolf and you're absolutely right! However, when I implement your suggestion, like so: local parts = getPlayer():getBodyDamage():getBodyParts(); for i=0, 16 do if parts[i] then parts[i]:RestoreToFullHealth() end if not parts[i] then getPlayer():Say("uh oh") end end , the behavior I witness in game is the player character repeatedly saying "uh oh" without any healing happening at all (I'm calling the function using this code with the OnPlayerUpdate event). This leads me to believe that maybe lua isn't capable of handling the BodyPartType types java is using in these cases? I'm no expert programmer, so I'm not sure this actually is the case, but it seems that every time I attempt to refer to a BodyPart object in lua, I see this kind of erroneous behavior Link to comment Share on other sites More sharing options...
Fenris_Wolf Posted September 23, 2018 Share Posted September 23, 2018 1 hour ago, AmbiguousMonk said: when I implement your suggestion, like so: ah sorry I see what I did there... my previous post was early my time and I may not have had enough coffee. The problem is getPlayer():getBodyDamage() is returning a java array. Doing: if parts[i] then parts[i]:RestoreToFullHealth() end Is trying to treat that java array as a lua table, which it's not. It should be used like: if parts:get(i) then parts:get(i):RestoreToFullHealth() end AmbiguousMonk 1 Link to comment Share on other sites More sharing options...
ProjectSky Posted September 23, 2018 Share Posted September 23, 2018 local Body = getPlayer():getBodyDamage() Body:RestoreToFullHealth() Link to comment Share on other sites More sharing options...
Fenris_Wolf Posted September 23, 2018 Share Posted September 23, 2018 (edited) 20 minutes ago, ProjectSky said: local Body = getPlayer():getBodyDamage() Body:RestoreToFullHealth() I was going to suggest this as a shortcut, however using the BodyDamage.RestoreToFullHealth method has other effects as well. It basically loops over all the BodyParts and calls a RestoreToFullHealth on them (as the initial poster was attempting to do), then runs additional code triggering other effects: removes pain resets endurance cures drunkenness cures colds removes wetness removes the zombie infection removes boredom removes unhappiness removes food sickness and resets the player temperature Edited September 23, 2018 by Fenris_Wolf Link to comment Share on other sites More sharing options...
AmbiguousMonk Posted September 24, 2018 Author Share Posted September 24, 2018 Thank you so much, Fenris_Wolf! That was exactly the solution to my problem. I greatly appreciate the help ProjectSky, thank you for your suggestion as well, although my goal isn't to actually use this code to heal the player. I was just using those calls as an example of the problem I was having while attempting to loop through each of the body parts. I apologize for not coming up with a better example 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