Player randomly disappears due to Animation

I have a Custom Animation Module that I am using to play KeyframeSequence Animations.

It works fine when playing a aniamtion but my problem is that any rig I use, it will randomly disappear and the descendants’ CFrame becomes nan (not a number). Another problem that happens is the entire game breaks and instances get destroyed.

Here is some of the code that animates the rig,
if you need more than just this feel free to ask.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

local Animator = require(script:WaitForChild("Animator")).new()

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

Character.Animate:Destroy()
Character.Humanoid.Animator:Destroy()

local Track = Animator:LoadAnimation(script:WaitForChild("Test"))
Track.Looped = true
Track:Play()

local RigList = {} :: { [string]: { [string]: Motor6D } }

for _, i in ipairs(Character:GetDescendants()) do
	if not i:IsA("Motor6D") then
		continue
	end
	RigList[i.Part0.Name] = RigList[i.Part0.Name] or {}
	RigList[i.Part0.Name][i.Part1.Name] = i
end

RunService.PreAnimation:Connect(function()
	task.desynchronize()
	Animator:Update()
	task.synchronize()

	for parentPoseName, childPoses in pairs(Animator._transforms) do
		for childPoseName, transform in pairs(childPoses) do
			if not RigList[parentPoseName] then
				continue
			end
			if not RigList[parentPoseName][childPoseName] then
				continue
			end
			RigList[parentPoseName][childPoseName].Transform = transform.CFrame
		end
	end
end)
1 Like

This may or may not be related to a certain ROBLOX Studio bug? Where you are dragging a part and it’s position suddenly gets set to nan,nan,nan and you can’t do anything except undo and trying again.

I don’t think it’s a Studio bug, but it does happen randomly. Most of the time it happens immediately or it can take up to a minute.