Hey, so I’m trying to figure out why sometimes my tools stop working, this usually occurs after resetting.
I eventually diagnosed this being a problem with LoadAnimation throwing an error, and it said something similar to: “Humanoid must be a descendant of the game object” I was very confused by this, this is the layout of my code:
local human = char:WaitForChild("Humanoid")
local animations = {harvest_R15 = (human:LoadAnimation(script:WaitForChild("HarvestR15"))), harvest_R6 = (human:LoadAnimation(script:WaitForChild("HarvestR6")))}
Character loading behaves in a stupidly puzzling manner. The character is not guaranteed to be parented to the Workspace when CharacterAdded fires, some character components are not guaranteed to be in the character when the character is parented and a whole lot of other mess.
Tools parented to the Backpack do actively run their code so that various events like Unequipped end up firing, so naturally that means the code may execute before the character is actually able to be used. Couple that with the above and you have a bit of work on your hands.
Somewhere in your code, before calling LoadAnimation, you will want to ensure that you’re only loading animations as in when the character is a valid descendant of the workspace using IsDescendantOf. If you want to avoid abusing loops, call a setup function upon the first equip and not again (e.g. if firstEquip then doStuff).
There was this that should’ve solved issues like this but it feels like it was cancelled:
I’ll never understand the mysteries of this game engine. By adding a print instead of a wait statement in my repeat loop, it seems to work consistently now.
It’s actually still broken. Something interesting is the repeat loop I implemented goes on forever, it never stops running. The condition I put in it was this:
I might’ve actually found a fix. I created a loop that’ll run a protected function call each frame, updating both the humanoid and character variables, then redefining the animations table.
Out of the many years I’ve been developing on this platform, I don’t recall really having an issue this stupid.
Roblox really needs to improve their poor character replication system. I mean, if CharacterAdded fires, I expect the character to have loaded in the game! Or better yet, my WaitForChild call on the Humanoid class in the character, that apparently finishes execution, but it’s parent is nil.
Very frustrated with Roblox right now, I’ve literally been working on this for well past 3 hours today…on this one issue…