Garamonde Posted March 8, 2022 Share Posted March 8, 2022 (edited) This is a little hard to explain, but here goes. For my character mod, K_K, he has an ability where when he begins doing any kind of work, such as chopping, mining, etc. nearby recruitable mobs (namely pigs and merms (only when wearing clever disguise)) will begin following him because he plays music (actual music not yet implemented). In short, he is a walking, somewhat-infinite, but situational, One-Man Band. The issue my friend and I are running into is that, a piece of code that's supposed to trigger a loss of sanity for K_K when one of his followers dies is causing the game to disconnect me from the server upon beginning to chop a tree (and I assume doing other forms of work as well, but haven't tested). The funny thing is that once I remove this code, the game runs fine. This is the piece of code in question: if inst.components.leader then --Make sure we are not messing with something that doesn't exist (For custom gamemodes and such) local oldAddFollower = inst.components.leader.AddFollower -- Store a backup of the AddFollower function for later use inst.components.leader.AddFollower = function(follower) -- Replace the AddFollower function with our own oldAddFollower(inst.components.leader, follower) -- Run the stored backup of the AddFollower function so we don't miss out on the original functionality of it follower:ListenForEvent("death", onFollowerDeath) --Run the "onFollowerDeath" function when the follower dies follower:ListenForEvent("stopfollowing", onFollowerLeave) --Run the "onFollowerLeave" function when the follower stops following us end end And here is his prefab file: kk.lua And here's the server log after one of those forced disconnects: server_log.txt Can anyone identify the problem(s) and perhaps show me how to fix it? Thank you! Edited March 9, 2022 by Garamonde Link to comment Share on other sites More sharing options...
Monti18 Posted March 8, 2022 Share Posted March 8, 2022 if inst.components.leader then --Make sure we are not messing with something that doesn't exist (For custom gamemodes and such) local oldAddFollower = inst.components.leader.AddFollower -- Store a backup of the AddFollower function for later use inst.components.leader.AddFollower = function(self,follower) -- Replace the AddFollower function with our own oldAddFollower(self, follower) -- Run the stored backup of the AddFollower function so we don't miss out on the original functionality of it follower:ListenForEvent("death", onFollowerDeath) --Run the "onFollowerDeath" function when the follower dies follower:ListenForEvent("stopfollowing", onFollowerLeave) --Run the "onFollowerLeave" function when the follower stops following us end end This is how the code should work. The problem was that you passed follower as the first argument, but for such functions self (the component itself) is always the first argument. Then you can also give the correct component to the old function 1 Link to comment Share on other sites More sharing options...
Garamonde Posted March 8, 2022 Author Share Posted March 8, 2022 THANK YOU!! I didn't realize the solution involved such minimal changes... It works perfectly! 1 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