Animate stuck in Running Anim After Ragdoll

So I have this Module Script to Ragdoll Players but afterwards, if the player was running while it happened which most likely will happen, the Animate starts breaking as it plays the Running Animation at he same time with everything else. Curious if anyone Knows a fix?

local RagdollHandler = {}
RagdollHandler.__index = RagdollHandler

local guiNames = {"InventoryGui", "HotbarGui"}

local function setInventoryGuiEnabled(player, enabled)
	local playerGui = player:FindFirstChildOfClass("PlayerGui")
	if not playerGui then return end

	for _, guiName in ipairs(guiNames) do
		local gui = playerGui:FindFirstChild(guiName)
		if gui then
			gui.Enabled = enabled
		end
	end
end

local function unequipTools(player)
	local character = player.Character
	local backpack = player:FindFirstChildOfClass("Backpack")
	if character then
		for _, tool in pairs(character:GetChildren()) do
			if tool:IsA("Tool") then
				tool.Parent = backpack or player
			end
		end
	end
end

local function storeJoints(character)
	local joints = {}
	for _, joint in pairs(character:GetDescendants()) do
		if joint:IsA("Motor6D") then
			table.insert(joints, {
				Name = joint.Name,
				Part0 = joint.Part0,
				Part1 = joint.Part1,
				C0 = joint.C0,
				C1 = joint.C1,
				Parent = joint.Parent
			})
		end
	end
	return joints
end

local function createRagdoll(character)
	for _, joint in pairs(character:GetDescendants()) do
		if joint:IsA("Motor6D") then
			local socket = Instance.new("BallSocketConstraint")
			local a1 = Instance.new("Attachment")
			local a2 = Instance.new("Attachment")

			a1.CFrame = joint.C0
			a2.CFrame = joint.C1
			a1.Parent = joint.Part0
			a2.Parent = joint.Part1

			socket.Attachment0 = a1
			socket.Attachment1 = a2
			socket.Parent = joint.Parent
			socket.LimitsEnabled = true
			socket.TwistLimitsEnabled = true

			joint:Destroy()
		end
	end
end


local function restoreJoints(jointData)
	for _, data in pairs(jointData) do
		local motor = Instance.new("Motor6D")
		motor.Name = data.Name
		motor.Part0 = data.Part0
		motor.Part1 = data.Part1
		motor.C0 = data.C0
		motor.C1 = data.C1
		motor.Parent = data.Parent
	end


	local model = jointData[1].Part0:FindFirstAncestorOfClass("Model")
	if model then
		for _, obj in pairs(model:GetDescendants()) do
			if obj:IsA("Attachment") or obj:IsA("BallSocketConstraint") then
				obj:Destroy()
			end
		end
	end
end


function RagdollHandler:Ragdoll()
	if self._isRagdolled then return end

	local player = game.Players:GetPlayerFromCharacter(self.Character)
	if player then
		setInventoryGuiEnabled(player, false)
		unequipTools(player)
		player:SetAttribute("Ragdolled", true)  -- Set attribute here
	end

	self._isRagdolled = true
	self._savedJoints = storeJoints(self.Character)
	createRagdoll(self.Character)

	local humanoid = self.Character:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.PlatformStand = true
	end

	-- Disable Animate script
	local animateScript = self.Character:FindFirstChild("Animate")
	if animateScript and animateScript:IsA("LocalScript") then
		self._animateScript = animateScript
		animateScript.Enabled = false
	end
end

function RagdollHandler:GetUp()
	if not self._isRagdolled then return end
	self._isRagdolled = false

	local player = game.Players:GetPlayerFromCharacter(self.Character)
	if player then
		player:SetAttribute("Ragdolled", false)  -- Reset attribute here
	end

	restoreJoints(self._savedJoints)

	local humanoid = self.Character:FindFirstChild("Humanoid")
	local rootPart = self.Character:FindFirstChild("HumanoidRootPart")

	if humanoid then
		humanoid.PlatformStand = false
		humanoid.WalkSpeed = 16  -- Reset walk speed to default
		humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)  -- Force animation reset
	end

	-- Re-enable Animate script with a short delay to properly reset animations
	if self._animateScript and self._animateScript.Parent == self.Character then
		self._animateScript.Enabled = false
		task.wait(0.1)
		self._animateScript.Enabled = true
	end

	if rootPart then
		local position = rootPart.Position
		rootPart.CFrame = CFrame.new(position) * CFrame.Angles(0, 2, 0)
		rootPart.AssemblyAngularVelocity = Vector3.zero
		rootPart.AssemblyLinearVelocity = Vector3.zero

		-- Zero velocities on all parts to avoid physics glitches
		for _, part in pairs(self.Character:GetDescendants()) do
			if part:IsA("BasePart") then
				part.AssemblyAngularVelocity = Vector3.zero
				part.AssemblyLinearVelocity = Vector3.zero
			end
		end
	end

	if player then
		setInventoryGuiEnabled(player, true)
	end
end

function RagdollHandler.new(character)
	local self = setmetatable({}, RagdollHandler)
	self.Character = character
	self._savedJoints = {}
	self._isRagdolled = false
	self._animateScript = nil
	return self
end

function RagdollHandler:RagdollFor(duration)
	if self._isRagdolled then return end
	self:Ragdoll()

	task.delay(duration, function()
		if self._isRagdolled then
			self:GetUp()
		end
	end)
end

return RagdollHandler
1 Like

Have you tried when the player ragdolls you check for all animations the player is doing (Presumably in another module script) and cancelling every animation before going into ragdoll?