Print not working after Wait()

This is part of a module script for ragdoll

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))
				wait(0.1)
				b:Destroy()
				--[[task.spawn(function()
					wait(.1)
					b:Destroy()
				end)--]]
			end
		end
		print("Bef")
		wait(3)
		print("Aft")
		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

When I call a function from this module, this code gets executed. Now the problem is, “Bef” prints, but “Aft” never prints. And of course anything after “Aft” doesn’t get executed too.

I am very confused, as between them there is literally nothing else but wait(), I have no idea why this could happen

perhaps during these three seconds you will delete this script

The script doesn’t get deleted, because later I am still able to call this function and it still prints “Bef”

If you remove the wait does it work. Also you should use task.wait()

I have tried using task.wait(), the problem didn’t go away. I thought that maybe the “task” is the issue so changed it back to wait().

Haven’t tried removing it, I’ll try, but it won’t solve my issue because I need the wait

Can you test the “Head” part of your script? It also has a wait, so does it destroy the Body Velocity?

It did destroy body velocity, I commented it out because again I thought that part might be the issue

Yeah but if it doesn’t work without the wait then the wait isn’t the issue maybe. Not sure what else it could be maybe the function is being overridden during the wait time. You could try using task.delay

As @TheDCraft said, does it work without the wait?

@TheDCraft , @STORMGAMESYT7IP I just tried. It does work without wait(). But with wait it doesn’t. I even added a debounce to the whole function so that it doesn’t get fired multiple times until everything inside it is finished. Still, with wait it doesnt work. Let me post here the whole script and the script where it calls the function

This is the Module Script inside ServerScriptService

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

Module.Ragdoll = function(char,bool)
	
	if ragDB == false then
		ragDB = true
		local plr = game.Players:GetPlayerFromCharacter(char)
		if char.Humanoid.Health~=0 and ((char:FindFirstChild'LowerTorso'and not char.LowerTorso.Root.Enabled)or (char:FindFirstChild'Torso'and not char.Torso.Neck.Enabled)) and not bool then
			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,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))
					wait(0.1)
					b:Destroy()
				--[[task.spawn(function()
					wait(.1)
					b:Destroy()
				end)--]]
				end
			end
			print("Bef")
			task.wait(3)
			print("Aft")
			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
		ragDB = false
	end
	
end

Module.Bot = function(bot)
	task.spawn(function()
		local H = bot.Humanoid
		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

And this is the script where I call the function

local boulder = script.Parent
local M = require(game.ServerScriptService.Module)
local prevHits = {}


boulder.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") and not prevHits[hit.Parent.Name] then
		
		table.insert(prevHits, hit.Parent.Name)
		M.Ragdoll(hit.Parent, true)
		
		
	end
	
end)

try removing wait(3), if it prints aft then it’s the wait issue if it doesn’t, then idk

You could try this:

task.delay(3, function()
   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)

Tried it, doesn’t work. I put a print function too right at the beginning at task.delay(), still nothing

Try wrapping everything inside the if statement in a coroutine (except the first line ragDB = true). I’ve had issues with waiting in module scripts in the past and that’s how i solved it

I have no idea how coroutines work, never used them before. Could you please show how?

Hopefully this shouldn’t mess up the debounce since it’s after it changes

Module.Ragdoll = function(char,bool)
	
	if ragDB == false then
		ragDB = true
		coroutine.resume(coroutine.create(function()
		local plr = game.Players:GetPlayerFromCharacter(char)
		if char.Humanoid.Health~=0 and ((char:FindFirstChild'LowerTorso'and not char.LowerTorso.Root.Enabled)or (char:FindFirstChild'Torso'and not char.Torso.Neck.Enabled)) and not bool then
			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,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))
					wait(0.1)
					b:Destroy()
				--[[task.spawn(function()
					wait(.1)
					b:Destroy()
				end)--]]
				end
			end
			print("Bef")
			task.wait(3)
			print("Aft")
			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
		ragDB = false
		end))
	end
	
end

Can you format you code properly paste it in studio then paste it here again. its confusing

The code is properly formatted, what are you talking about?

image