Task.wait() is not running

I’m making a Stun System that stuns the player using a ModuleScript. But the script is stopping to work in a wait.

local StunHandler = {}

function StunHandler.Stun(Type, AffectEnemy, Enemy)
	local Humanoid = script.Parent.Parent.Parent:WaitForChild("Humanoid")
	local Char = script.Parent.Parent.Parent
	local ValorBase = script.Parent.Parent:WaitForChild("PlayerHandler").WalkSpeed
	local Stunado = script.Parent.Parent.Parent:WaitForChild("PlayerValues").Stunned
	local Ts = game:GetService("TweenService")
	local Debris = game:GetService("Debris")
		if Type == "Ragdoll" then
				local FirstSound = game.Workspace.Sons.Ragdoll:Clone()
				FirstSound.Parent = Humanoid.Parent.HumanoidRootPart
				FirstSound:Play()
				Stunado.Value = true
				Humanoid.WalkSpeed = 0
				script.Parent.Parent.Parent:WaitForChild("StandHandler").IsActive.Value = false
				for z, trackin in pairs(Humanoid.Animator:GetPlayingAnimationTracks()) do
					if trackin then
						trackin:Stop()
					end
				end
				local Stando = Char:FindFirstChild("Stand")
				if Stando then
					local PP = Char:GetChildren()
					local MainPart = Stando.PrimaryPart
					for i, bodyPart in pairs(Stando:GetDescendants()) do			
						if bodyPart:IsA("BasePart") or bodyPart:IsA("MeshPart") then
							if bodyPart ~= MainPart then
								local info = TweenInfo.new(0.6)
								local goal = {Transparency = 1}

								local tween = Ts:Create(bodyPart, info, goal)
								tween:Play()
							end
						end
					end
					for z = 1, #PP do
						if PP[z].className == "MeshPart" then
							if PP[z].UserParticle then
								PP[z].UserParticle:Destroy()
							end
						end
					end
					Stando.PrimaryPart.StandAttach.AlignPosition.Attachment1 = Char.HumanoidRootPart.RootRigAttachment
					Stando.PrimaryPart.StandAttach.AlignPosition.Responsiveness = 50
					local Sound = game.ReplicatedStorage.CommomAnimations.Sounds.Unsummon:Clone()
					Sound.Name = "AnotherUnsum"
					Sound.Parent = Humanoid.Parent.HumanoidRootPart
					Sound:Play()
					Sound.Stopped:Connect(function()
						wait(.1)
						FirstSound:Destroy()
						Sound:Destroy()
					end)
					Debris:AddItem(Stando, 0.5)
				end
				Humanoid.Sit = true
				for i,v in pairs(Char:GetDescendants()) do
					if v:IsA("Motor6D") and v.Parent.Name ~= "HumanoidRootPart" then
						local Socket = Instance.new("BallSocketConstraint")
						local a1 = Instance.new("Attachment")
						local a2 = Instance.new("Attachment")
						a1.Parent = v.Part0
						a2.Parent = v.Part1
						Socket.Parent = v.Parent
						Socket.Attachment0 = a1
						Socket.Attachment1 = a2
						a1.CFrame = v.C0
						a2.CFrame = v.C1
						Socket.LimitsEnabled = true
						Socket.TwistLimitsEnabled = true
						v:Destroy()
					end
				end
		Humanoid.RequiresNeck = false
		print("before-task") -- here as can you see i did put a print to see when the script stopped to work
		task.wait(3.5)
		print("task")
				for i,v in pairs(Char:GetDescendants()) do
					if v:IsA("BallSocketConstraint") then
						v.UpperAngle = 0
						v.TwistUpperAngle = 0
						v.TwistLowerAngle = 0
						local Joints = Instance.new("Motor6D",v.Parent)
						Joints.Part0 = v.Attachment0.Parent
						Joints.Part1 = v.Attachment1.Parent
						Joints.C0 = v.Attachment0.CFrame
						Joints.C1 = v.Attachment1.CFrame
						v:Destroy()
					end
				end
				Humanoid.Sit = false
				Humanoid.WalkSpeed = ValorBase.Value
		Stunado.Value = false
		print("sucess")
		end
end

return StunHandler

As you can see in the image below the input isnt printing the print(“task”) after the task.wait(3.5). Also there are no errors in server or client logs.
I tried to change it to wait(3.5) but it didn’t work too.

Hi Mate not sure if you have tried this :

elapsed task.wait(duration=0)

or

local elapsedTime = task.wait(2) – wait for 2 seconds
print(elapsedTime)

or this :

task.wait()
– is equivalent in behavior to
RunService.Heartbeat:Wait()
:+1:

1 Like

Can u Show me the script which uses this function?

1 Like

Try replacing the task.wait() and whatever is after until the end with this

delay(3.5, function() 
	print("task")
				for i,v in pairs(Char:GetDescendants()) do
					if v:IsA("BallSocketConstraint") then
						v.UpperAngle = 0
						v.TwistUpperAngle = 0
						v.TwistLowerAngle = 0
						local Joints = Instance.new("Motor6D",v.Parent)
						Joints.Part0 = v.Attachment0.Parent
						Joints.Part1 = v.Attachment1.Parent
						Joints.C0 = v.Attachment0.CFrame
						Joints.C1 = v.Attachment1.CFrame
						v:Destroy()
					end
				end
				Humanoid.Sit = false
				Humanoid.WalkSpeed = ValorBase.Value
		Stunado.Value = false
		print("sucess")
end) 

The reason is that the script probably gets destroyed during the task.wait

3 Likes

Here is the script:
In this case, check the part thats says “Heavy Punch”, where calls “EnemyStun.Stun(“Ragdoll”, false, nil)”

local Debounce = false
local Player = script.Parent.Parent.Parent
local Enemys = {}
local Executing = false
local Type = script.Parent.Type
local StunHandler = require(Player.PlayerScripts.StunSystem.StunHandler)
local StandValue = Player.StandHandler.Stand
local Folder = game.ReplicatedStorage.StandFolder:WaitForChild(StandValue.Value)
local Dano = Folder.Damages:WaitForChild(Type.Value)
local HealMode = script.Parent.HealMode

script.Parent.Touched:Connect(function(hit)
	if Debounce == false then
		if hit.Parent:FindFirstChild("Humanoid") then
			local Humanoid = hit.Parent:FindFirstChild("Humanoid")
			local LoadStun = Humanoid.Animator:LoadAnimation(game.ReplicatedStorage.CommomAnimations.CharAnims.Stun)
			if Humanoid.Parent.Name ~= Player.Name then
				Executing = true

				local Achar = table.find(Enemys, Humanoid.Parent.Name) 
				local Enemy = Humanoid.Parent
				local HumRp = Humanoid.Parent.HumanoidRootPart

				local function KnockBack()
					if Humanoid:FindFirstChild("Target") then
						local Target = Humanoid.Target
						Target.Value = Player.Name
					end
					if Type.Value == "Barrage" then
						
						LoadStun.Priority = Enum.AnimationPriority.Action4
						if LoadStun.IsPlaying == false then
							LoadStun:Play()
						end

						Humanoid.WalkSpeed = 3
						if Humanoid.Parent:FindFirstChild("PlayerValues") then
							Humanoid.Parent.PlayerValues.StunnedType.Value = true
							Humanoid.Parent.PlayerValues.Stunned.Value = true
							Humanoid.Parent.PlayerScripts.StunSystem.CantJump.Disabled = false
							spawn(function()
								task.wait(1.5)
								LoadStun:Stop()
								Humanoid.Parent.PlayerValues.StunnedType.Value = false
								Humanoid.Parent.PlayerValues.Stunned.Value = false
								Humanoid.Parent.PlayerScripts.StunSystem.CantJump.Disabled = true
								Humanoid.WalkSpeed = Humanoid.Parent.PlayerScripts.PlayerHandler.WalkSpeed.Value
							end)
						end

						local target = HumRp
						local BV = Instance.new("BodyVelocity")

						BV.P = math.huge
						BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
						BV.Velocity = target.CFrame.lookVector * -2
						BV.Parent = target

						game.Debris:AddItem(BV, 0.5)
					elseif Type.Value  == "Punch" then
						
						LoadStun.Priority = Enum.AnimationPriority.Action4
						if LoadStun.IsPlaying == false then
							LoadStun:Play()
						end

						Humanoid.WalkSpeed = 3
						if Humanoid.Parent:FindFirstChild("PlayerValues") then
							Humanoid.Parent.PlayerValues.StunnedType.Value = true
							Humanoid.Parent.PlayerValues.Stunned.Value = true
							Humanoid.Parent.PlayerScripts.StunSystem.CantJump.Disabled = false
							spawn(function()
								task.wait(1.5)
								LoadStun:Stop()
								Humanoid.Parent.PlayerValues.StunnedType.Value = false
								Humanoid.Parent.PlayerValues.Stunned.Value = false
								Humanoid.Parent.PlayerScripts.StunSystem.CantJump.Disabled = true
								Humanoid.WalkSpeed = Humanoid.Parent.PlayerScripts.PlayerHandler.WalkSpeed.Value
							end)
						end
						
						local target = HumRp
						local BV = Instance.new("BodyVelocity")

						BV.P = math.huge
						BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
						BV.Velocity = target.CFrame.lookVector * -5.5
						BV.Parent = target

						game.Debris:AddItem(BV, 0.5)
					elseif Type.Value  == "HeavyPunch" then
						
						LoadStun.Priority = Enum.AnimationPriority.Action4
						if LoadStun.IsPlaying == false then
							LoadStun:Play()
						end

						Humanoid.WalkSpeed = 3
						if Humanoid.Parent:FindFirstChild("PlayerValues") then
							Humanoid.Parent.PlayerValues.StunnedType.Value = true
							Humanoid.Parent.PlayerValues.Stunned.Value = true
							Humanoid.Parent.PlayerScripts.StunSystem.CantJump.Disabled = false
							spawn(function()
								task.wait(1.5)
								LoadStun:Stop()
								Humanoid.Parent.PlayerValues.StunnedType.Value = false
								Humanoid.Parent.PlayerValues.Stunned.Value = false
								Humanoid.Parent.PlayerScripts.StunSystem.CantJump.Disabled = true
								Humanoid.WalkSpeed = Humanoid.Parent.PlayerScripts.PlayerHandler.WalkSpeed.Value
							end)
						end
						
						local target = HumRp
						local BV = Instance.new("BodyVelocity")

						BV.P = math.huge
						BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
						BV.Velocity = target.CFrame.lookVector * -30
						BV.Parent = target

						game.Debris:AddItem(BV, 0.5)
					end
				end
				
				Debounce = true

				if Type.Value == "Barrage" then
					if Enemy:FindFirstChild("StandHandler") then
						local EnemyValues = Enemy.StandHandler
						local Blocking = EnemyValues.Blocking
						if HealMode.Value == false then
							if Blocking.Value == false then
								Humanoid:TakeDamage(Dano.Value)
								KnockBack()
								wait(.2)
								Debounce = false
							end
						else
							Humanoid.Health += Dano.Value
							KnockBack()
							wait(.2)
							Debounce = false
						end
					else
						if HealMode.Value == false then
							Humanoid:TakeDamage(Dano.Value)
							KnockBack()
							wait(.2)
							Debounce = false
						else
							Humanoid.Health += Dano.Value
							KnockBack()
							wait(.2)
							Debounce = false
						end
					end
				elseif Type.Value == "HeavyPunch" and not Achar then
					if Enemy:FindFirstChild("StandHandler") then
						local EnemyValues = Enemy.StandHandler
						local Blocking = EnemyValues.Blocking
						local PB = EnemyValues.PB
						table.insert(Enemys, Humanoid.Parent.Name)
						if HealMode.Value == false then
							if Blocking.Value == true then
								if PB.Value == true then
									StunHandler.Stun("PBStun", true, Humanoid.Parent)
								else
									Humanoid:TakeDamage(10)
									local EnemyStunHandler = require(Humanoid.Parent.PlayerScripts.StunSystem.StunHandler)
									EnemyStunHandler.Stun("BBStun", false, nil)
								end
							else
								KnockBack()
								Humanoid:TakeDamage(Dano.Value)
								local EnemyStunHandler = require(Humanoid.Parent.PlayerScripts.StunSystem.StunHandler)
								EnemyStunHandler.Stun("Ragdoll", false, nil)
							end
						else
							KnockBack()
							Humanoid.Health += Dano.Value
							local EnemyStunHandler = require(Humanoid.Parent.PlayerScripts.StunSystem.StunHandler)
							EnemyStunHandler.Stun("Ragdoll", false, nil)
						end
					else
						if HealMode.Value == false then
							Humanoid:TakeDamage(Dano.Value)
							KnockBack()
						else
							Humanoid.Health += Dano.Value
							KnockBack()
						end
					end
				elseif Type.Value == "Punch" and not Achar then
					if Enemy:FindFirstChild("StandHandler") then
						local EnemyValues = Enemy.StandHandler
						local Blocking = EnemyValues.Blocking
						if HealMode.Value == false then
							if Blocking.Value == false then
								table.insert(Enemys, Humanoid.Parent.Name)
								Humanoid:TakeDamage(Dano.Value)
								KnockBack()
							end
						else
							table.insert(Enemys, Humanoid.Parent.Name)
							Humanoid.Health += Dano.Value
							KnockBack()
						end
					else
						if HealMode.Value == false then
							Humanoid:TakeDamage(Dano.Value)
							KnockBack()
						else
							Humanoid.Health += Dano.Value
							KnockBack()
						end
					end
				end
			end
		end
	end
end)

I think no cuz I don’t destroy or change it in any part of any script.

Is the script in the character? Does the character die and respawn?

Yes, it is. I’ll try to change the script to ServerScriptService and change the variables and use new parameters to acess the player.