Tool behavior is weird after getting ragdolled

So I have a module script that ragdolls player. The script works but it breaks the character and tool interaction

Heres video examples:
This is how the tool should work. (Pressing left mouse rotates character left and pressing right mouse rotates character to right)

Working Clip - YouTube ( I couldnt upload the video here so i am using youtube )

But this is what happens right after ragdolls:

Issue #1:
Character gets flinged even if i press nothing

Issue #2:

Weird walk animation bug, character rotates itself to right during ult (even if i press no mouse button)

Issue #3:
If i equip tool after getting ragdolled, it teleports me

I have checked collisions and everything multiple times, even tried to destroy and clone during ragdoll but nothing works, heres the ragdoll module

(I am uploading image for code and whole code in text below images so you can read whatever you prefer)




image

Text code:

local Ragdollmod = {}
local debris = game:GetService("Debris")
function Ragdollmod.Ragdoll(FORCEX,FORCEY,FORCEZ ,SPEED,target, Scriptholder,Cframepart, Disableknockback, ragduration, AbilityHit, Player)
	--local tool = target.Parent:FindFirstChildWhichIsA("Tool")
	
	--local toolname
	--local findtool 
	--if tool ~= nil then
	--	toolname = tool.Name
	--	findtool = game.ServerStorage.Tools:FindFirstChild(toolname)
	--	if findtool ~= nil then 
	--		tool:Destroy()
	--		print("destroyed")
--		end
	--end
	if not AbilityHit then
		if target.Parent.ValueFolder.CanKnockback.Value == false then return end
	end
	local realplayer
	target.Parent.ValueFolder:FindFirstChild("CanDash").Value = false
	target.Parent.ValueFolder:FindFirstChild("CanSprint").Value = false
	if Player ~= nil and Player:IsA("StringValue") then -- hit by hammer ability
		realplayer = game.Players:FindFirstChild(Player.Value)
		local mainFolder = realplayer:FindFirstChildWhichIsA("Folder")
		local killCount = mainFolder.KillCount
		local Abilityhitcount = mainFolder.AbilityHitCount
		local Hitcount = mainFolder.HitCount
		local oldtag = target:FindFirstChild("creator")
		if oldtag  then
			oldtag:Destory()
		end
		local Tagged = Instance.new("ObjectValue")
		Tagged.Name = "creator"
		Tagged.Value = realplayer
		Tagged.Parent = target
		game.Debris:AddItem(Tagged, 5)

		if AbilityHit then 
			Abilityhitcount.Value += 1
		else 
			Hitcount.Value += 1
		end
	elseif Player ~= nil then 
		realplayer = game.Players:FindFirstChild(Player.Name)
		local mainFolder = realplayer:FindFirstChildWhichIsA("Folder")
		local killCount = mainFolder.KillCount
		local Abilityhitcount = mainFolder.AbilityHitCount
		local Hitcount = mainFolder.HitCount
		local Tagged = Instance.new("ObjectValue")
		Tagged.Name = "creator"
		Tagged.Value = realplayer
		Tagged.Parent = target
		game.Debris:AddItem(Tagged, 5)
		if AbilityHit then 
			Abilityhitcount.Value += 1
		else 
			Hitcount.Value += 1
		end
	end



	if Disableknockback then 
		target.Parent.ValueFolder.CanKnockback.Value = false
	else
		target.Parent.ValueFolder.CanKnockback.Value = false
		target.Parent.ValueFolder.CanKnockback.Value = true
	end
	target.PlatformStand = true
	local knockback = Instance.new('BodyVelocity',target.Parent:WaitForChild("HumanoidRootPart"))

	knockback.MaxForce = Vector3.new(FORCEX,FORCEY,FORCEZ)
	if Scriptholder:IsA("Tool") then -- Hit by basic attack
		knockback.Velocity = Scriptholder.Handle.CFrame.LookVector * SPEED
		knockback.Name = "KnockBack"
	else -- Hit by ability
		knockback.Velocity = Cframepart.CFrame.LookVector * SPEED
		knockback.Name = "KnockBack"
	end
	local humanoid = target
	humanoid.BreakJointsOnDeath = false
	humanoid.RequiresNeck=true
	humanoid:ChangeState(Enum.HumanoidStateType.Physics)
	for _, v in pairs(target.Parent:GetDescendants()) do
			if v:IsA("Part") then
				v.Massless = true
			end
		if v:IsA("Motor6D") then

				local a0, a1 = Instance.new("Attachment"), Instance.new("Attachment")
				a0.CFrame = v.C0
				a1.CFrame = v.C1
				a1.Name = "Fordestroy"
				a0.Parent = v.Part0
				a1.Parent = v.Part1
				a0.Name = "Fordestroy"
				local b = Instance.new("BallSocketConstraint")
				b.Name = "Fordestroy"
				b.Attachment0 = a0
				b.Attachment1 = a1
				b.Parent = v.Part0
				v.Enabled = false
			end
	end
	target:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
	task.wait(.1)
	knockback:Destroy()
	if ragduration == nil then
		wait(2)
	else 
		wait(ragduration)
	end
	target.PlatformStand = false
	for _,v in pairs(target.Parent:GetDescendants()) do
		if v.Name == 'Fordestroy' then
			v:Destroy()
		end
		if v:IsA('Motor6D') then
				v.Enabled = true
			end
			if v:IsA("Part") then
				v.Massless = true
			end
			
	end
	wait(.5)
	target.Parent.ValueFolder:FindFirstChild("CanDash").Value = true
	target.Parent.ValueFolder:FindFirstChild("CanSprint").Value = true
	humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
	--findtool:Clone().Parent = target.Parent
end

Any help will be appreciated, I’ve been having problem with this for like 2 weeks

I am ready to provide any information with this.

1 Like