Problem with ragdoll module

I am currently using a ragdoll module that has a function to ragdoll a player for a few seconds before unragdolling them. However, for some reason, when I use a specfic script, the ragdoll module stops working at the wait() function and the player is left to ragdoll infinitely. This does not happen with other scripts using the ragdoll module, so why is this happening?

Ragdoll Module:

local Module = {}
local RE = game.ReplicatedStorage.Events.Ragdoll
local PS = game:GetService("PhysicsService")

Module.Ragdoll = function(char,timer)
	local plr = game.Players:GetPlayerFromCharacter(char)
	if char.Humanoid.Health~=0 and ((char:FindFirstChild'LowerTorso'and char.LowerTorso.Root.Enabled) or (char:FindFirstChild'Torso'and char.Torso.Neck.Enabled)) and timer then
		RE:FireClient(plr,true)
		for _,v in pairs(char:GetDescendants()) do
			if v:IsA'Motor6D'and Module.Check(v.Name)then
				v.Enabled = false
			elseif v:IsA'BallSocketConstraint' then
				v.Enabled = true
			elseif v.Name=="Head"then
				local b = Instance.new("BodyVelocity",char.Head)
				b.Velocity = Vector3.new(math.random(-10,10),0,math.random(-10,10))
				task.spawn(function()
					wait(.1)
					b:Destroy()
				end)
			end
		end
		
		print(timer) -- This is printed by the broken script
		wait(timer)
		print("waited") -- This is never printed by the broken script...
		
		RE:FireClient(plr,false)
		for _,v in pairs(char:GetDescendants()) do
			if v:IsA'Motor6D' then
				v.Enabled = true
			elseif v:IsA'BallSocketConstraint' then
				v.Enabled = false
			end
		end
	else
		RE:FireClient(plr,false)
		for _,v in pairs(char:GetDescendants()) do
			if v:IsA'Motor6D' then
				v.Enabled = true
			elseif v:IsA'BallSocketConstraint' then
				v.Enabled = false
			end
		end
	end
end

Module.Bot = function(bot)
	task.spawn(function()
		local H = bot.Humanoid
		bot.HumanoidRootPart:SetNetworkOwner(nil)
		if bot:FindFirstChild'HumanoidRootPart'and H.Health~=0 and bot:FindFirstChild'LowerTorso' and bot.LowerTorso.Root.Enabled==true then
			H:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
			H:ChangeState(Enum.HumanoidStateType.Ragdoll)
			for _,v in pairs(H:GetPlayingAnimationTracks())do v:Stop(0)end
			bot.Animate.Disabled = true
			
			for _,v in pairs(bot:GetDescendants()) do
				if v:IsA'Motor6D'and Module.Check(v.Name) then
					v.Enabled = false
				elseif v:IsA'BallSocketConstraint' then
					v.Enabled = true
				end
			end
			wait(10)

			bot.Animate.Disabled = false
			H:SetStateEnabled(Enum.HumanoidStateType.GettingUp,true)
			H:ChangeState(Enum.HumanoidStateType.GettingUp)

			for _,v in pairs(bot:GetDescendants()) do
				if v:IsA'Motor6D' then
					v.Enabled = true
				elseif v:IsA'BallSocketConstraint' then
					v.Enabled = false
				end
			end
		end
	end)
end

Module.Check = function(t)
	for _,v in pairs({"LeftWrist","RightWrist","LeftAnkle","RightAnkle"}) do
		if v == t then return false end
	end
	return true
end

Module.Joints = function(c)
	c.Humanoid.BreakJointsOnDeath = false
	c.Humanoid.RequiresNeck = false
	for _,v in pairs(c:GetDescendants()) do
		if v:IsA("Motor6D")and Module.Check(v.Name)then
			local b = Instance.new("BallSocketConstraint",v.Parent)
			local a0,a1 = Instance.new("Attachment"),Instance.new("Attachment")
			a0.Parent,a1.Parent = v.Part0,v.Part1
			b.Attachment0,b.Attachment1 = a0,a1
			a0.CFrame,a1.CFrame = v.c0,v.c1
			b.LimitsEnabled = true
			b.TwistLimitsEnabled = true
			b.Enabled = false
		elseif v:IsA'BasePart' then
			PS:SetPartCollisionGroup(v,"A")
			if v.Name == "HumanoidRootPart" then
				PS:SetPartCollisionGroup(v,"B")
			elseif v.Name=="Head"then
				v.CanCollide = true
			end
		end
	end
end

return Module

Script calling out the ragdoll module:

-- This script is located inside a spike part that is coded to be cloned and appear out of the ground randomly every once in a while
local part = script.Parent
local warning = part.Parent
local plrTbl = {}
local ts = game:GetService("TweenService")
local tinfo = TweenInfo.new(0.2)

part.Position = warning.Position - Vector3.new(0, 5, 0)

local function rag(char)
	require(game:GetService("ServerScriptService").RagdollModule).Ragdoll(char, 2)
	--char.HumanoidRootPart:ApplyImpulse(Vector3.new(0, 100, 0))
end

part.Touched:Connect(function(partTouched)
	local char = partTouched.Parent
	local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
	if plr and not table.find(plrTbl, plr) then
		table.insert(plrTbl, plr)
		rag(char)
	end
end)

ts:Create(part, tinfo, {Position = warning.Position + Vector3.new(0, 5, 0)}):Play()

wait(1)
script.Enabled = false

Heres a reference of the ragdoll module I am using in case it is needed (I made some changes to the original script)

Thanks for reading through, I know it is pretty long.

Edit: If you have any ragdoll module suggestions, please tell me! The ragdoll module I am using can be a bit buggy at times.

1 Like

the script is applying a ragdoll onto a character for a duration of 2 seconds once the spike is touched

but one second later after the script was initialized you disable it which terminates every single running proccess of that script including the wait for ragdoll to finish and the disabling of it if there is any

to fix this you can simply disconnect the Touched event connection instead of disabling the entire script

local Connection
Connection = Part.Touched:Connect(funcion()
	. . .
end)

task.wait(1)
Connection:Disconnect()
1 Like

How does disconnect work? I haven’t used it before

Also thanks your solution worked I didn’t know that functions in modules are ran on the script that is using the module and not on the module itself

2 Likes

disconnect simply unlists a function from being executed once an event occurs. Every time you use :Connect() method you are adding a new function to a, lets say so, to-do list for that event

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.