I am trying to make a system where the player can select existing rigs in the workspace to switch control over to them and use them like a normal character. I have seen this accomplished once before in the game Split, but since I do not have access to its code I have had to build it from the ground up. It works for the most part, but when the player switches out of a character that character’s animations start to break.
Since I didn’t receive member status on the forums until yesterday, I asked the studio AI assistant what the source of the problem could be and it said the issue likely came from the character reparenting from workspace to nil, then back, which can be seen in the previously linked clip as the character suddenly flickering out of existence upon switching. I am unsure if this is the actual cause, but it is the only lead I have, though even if it isn’t related, the flickering is another issue that needs to be fixed.
There are plenty of other bugs in my system currently, but this one is currently the most problematic.
It depends on how you’re writing this. Are you reparenting the character to workspace (like the AI assistant said)? If so, then this is expected behavior.
To solve this, you need to find a way to switch characters without refreshing it
Reparenting it to the workspace is the only way to go about it I can think of as of right now since to my knowledge switching the character forces the original to be deleted as part of Roblox’s built in behavior.
The only workaround I can think of is not switching the character at all, and instead just relaying the player’s inputs to the selected character, but this seems like a very difficult and resource intensive way to go. I’d like to keep that method as a last resort.
I could definitely solve the flickering by making an effect where the character intentionally flickers by changing transparency, but that still leaves the issue with the animations being broken afterwards.
I have tried disabling the animation script during the switch, but that doesn’t seem to work, which leads me to believe it is an issue with the actual rig and not the animations. I have very limited knowledge with animations, though, so I have no clue what part of the setup the problem would be related to.
Hard to say what’s causing it, but from first glance, my intuition is telling me that it may be something to do with multiple copies of the same animation being loaded onto the animator which might cause the animations to sort of overlap each other.
You can confirm if this is the case by printing out all the playing animations in the animator while it’s walking to see if there’s multiple of the same animation playing at once. Should look something like this:
local animator = character.Humanoid.Animator
for _, playingAnimation in animator:GetPlayingAnimationTracks() do
print(playingAnimation)
end
If this is the case, you would probably have to replace the ‘Animator’ object with a new one, and parent a new copy of the ‘Animate’ local script in the new rig each time you swap bodies.
Running the script on an npc while standing still and using it for the first time yields Animation1, while running it the second time around gives Animation1, Animation, and Animation1., which sounds like what you were talking about.
Upon further investigation, it looks like the issue only occurs for the clients(as in every client at the same time) and not on the server side, which is interesting but makes no difference since nobody can see what the server sees. Deleting and replacing the Animator of the affected Humanoid fixes the issue without anything needed to be done to the actual script.