Character bounces once ragdoll is finished

The title basically says it all, once the player is unragdolled, it flings the character for like 0.1 seconds then teleports it back to the ragdoll position before the getting up animation is played, this is a video of what happens.

This is my code:

LocalScript in StarterCharacterScripts

local playerService = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local camera = game.Workspace.CurrentCamera
local TweenService = game:GetService("TweenService")

local goal = {}
goal.FieldOfView = 100

local goal2 = {}
goal2.FieldOfView = 75

local tweenInfo = TweenInfo.new(2)
local tweenInfo2 = TweenInfo.new(0.2)

local tween = TweenService:Create(camera, tweenInfo, goal)
local tween2 = TweenService:Create(camera, tweenInfo2, goal2)

-- Get the player's critical parts.
local player = playerService.LocalPlayer
local char = player.Character
while char == nil do
	task.wait()
	char = player.Character
end
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local ragdolled = char:WaitForChild('Ragdolled')

local anim = script.Anim
local playAnim = char:WaitForChild("Humanoid"):LoadAnimation(anim)

local fallingAnimation = script.FallingAnim
local playfallanim = char:WaitForChild("Humanoid"):LoadAnimation(fallingAnimation)


hum.StateChanged:Connect(function(OldState, NewState)
	if NewState == Enum.HumanoidStateType.Freefall then
		task.spawn(function()
			local humHeight = root.Position.Y
			repeat
				task.wait()
			until root.Position.Y <= humHeight - 16
			camera.CameraSubject = char:WaitForChild("UpperTorso")
			tween:Play()
			workspace.Wind:Play()
			playfallanim:Play()
			player.Character:WaitForChild("Humanoid").WalkSpeed = 0
			player.Character:WaitForChild("Humanoid").AutoRotate = false
		end)
	elseif NewState ~= Enum.HumanoidStateType.Freefall and NewState ~= Enum.HumanoidStateType.Jumping then
		if hum:GetState() == Enum.HumanoidStateType.Landed and playAnim.IsPlaying == false and playfallanim.IsPlaying == true then
			tween2:Play()
			workspace.Wind:Stop()
			playfallanim:Stop()
			player.Character:WaitForChild("Humanoid").JumpPower = 0
			replicatedStorage.RagdollServer:FireServer(char)
		elseif hum:GetState() == Enum.HumanoidStateType.Physics then
			task.delay(5, function()
				if hum:GetState() == Enum.HumanoidStateType.Physics then
					camera.CameraSubject = char:WaitForChild("Humanoid")
					replicatedStorage.GetUp:FireServer(char)
					playfallanim:Stop()
					playAnim:Play()
					wait(1.7)
					playfallanim:Stop()
					player.Character:WaitForChild("Humanoid").JumpPower = 50
					player.Character:WaitForChild("Humanoid").WalkSpeed = 7
					player.Character:WaitForChild("Humanoid").AutoRotate = true
				end
			end)
			
			end
	end
end)

local function change_state()
	if ragdolled.Value then
		hum:ChangeState(Enum.HumanoidStateType.Physics)
	else
		hum:ChangeState(Enum.HumanoidStateType.GettingUp)
	end
end

ragdolled.Changed:Connect(change_state)

ModuleScript in ServerScriptService

local module = {}

function module.ragdoll(character)
	if character.Ragdolled.Value then return end
	
	character.Ragdolled.Value = true
	character:WaitForChild("HumanoidRootPart").CanCollide = false
	
	for i, joint in pairs(character:GetDescendants()) do
		if joint:IsA('Motor6D') then
			local socket = Instance.new('BallSocketConstraint', joint.Parent)
			local att0 = Instance.new('Attachment', joint.Part0)
			local att1 = Instance.new('Attachment', joint.Part1)
			
			att0.CFrame = joint.C0
			att1.CFrame = joint.C1
			
			socket.Attachment0 = att0
			socket.Attachment1 = att1
			socket.TwistLimitsEnabled = true
			socket.LimitsEnabled = true
			
			joint.Enabled = false
		end
	end
end

function module.unragdoll(character)
	
	character.Ragdolled.Value = false
	character:WaitForChild("HumanoidRootPart").CanCollide = true

	for i, joint in pairs(character:GetDescendants()) do
		if joint:IsA('Motor6D') then
			joint.Enabled = true
		elseif joint:IsA('BallSocketConstraint') then
			joint:Destroy()
		end
	end
	
end

return module

Script in ServerScriptService

local RagdollHandler = require(script.Parent:WaitForChild("RagdollHandler"))
local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local ragdolled = Instance.new('BoolValue', character)
		ragdolled.Name = "Ragdolled"
	end)
end)

ReplicatedStorage.RagdollServer.OnServerEvent:Connect(function(player, character)
	RagdollHandler.ragdoll(character)
end)

ReplicatedStorage.GetUp.OnServerEvent:Connect(function(player, character)
	RagdollHandler.unragdoll(character)
end)
2 Likes