Why does task.wait() completly stops the function?

I use this script for a melee weapon, it activates when the tool is activated.

The script works great, except when I use a task.wait() it completely stops the function it’s in.
So yeah if anyone can help I’d be appreciated

Code:

debounce = false
Tool = script.Parent
handle = Tool.Handle
Debris = game:GetService("Debris")
Players = game:GetService("Players")
hand = Tool.Parent:FindFirstChild("RightHand")

function getPlayerFromCharacter(character)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == character then
			return player
		end
	end
end
function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = getPlayerFromCharacter(Tool.Parent)
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end
function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

function touched(whoTouched)
  local humanoid = whoTouched.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid ~= nil and debounce == false then
		debounce = true
		handle.stab:Play()
		local character = whoTouched.Parent
		local player = Players:getPlayerFromCharacter(character)
		local Player = Players:getPlayerFromCharacter(Tool.Parent)
		if player and (player == Player or IsTeamMate(Player, player) and Tool.CanFriendlyFire.Value == false)then
			return end
		if script.Parent.TagsPlayer.Value == true then
			UntagHumanoid(humanoid)
			TagHumanoid(humanoid, Player)
		end
		
		local weld = Instance.new("Weld")
		weld.Parent = character
		weld.Part0 = hand
		weld.Part1 = character.HumanoidRootPart
		humanoid:TakeDamage(40)
		task.wait(1)-- Script stops working here
		weld:Destroy()
		
	end
end
function Equipped()
	local Character = Tool.Parent
	Player = Players:getPlayerFromCharacter(Character)
end

handle.Touched:Connect(touched)

3 Likes

Hey, You’re using a debounce variable to prevent the touched function from running multiple times in quick succession. After the touched function is triggered, debounce is set to true , which means any subsequent touches will be ignored until debounce is set back to false . However, I noticed that you never reset debounce back to false after the task.wait(1) line. This means that after the first touch, the touched function will never run again. To fix this, you should reset debounce after the wait

task.wait(1)
weld:Destroy()
debounce = false

3 Likes

Put it in the script, but this does not work sadly
I can still repeat the function but it just does not work past task.wait()

1 Like

I think the touched function disconnects when a new part touches it which may cause the task.wait to not work

Can you help me on how to fix it?

1 Like

Maybe make a new function, and make the task.wait there. Also dont forget to call it in the end of the touched function

1 Like

Tried it like this, did not work.

function this()
   task.wait(1)
   print("worked")
end

function touched(whoTouched)
 local humanoid = whoTouched.Parent:FindFirstChildOfClass("Humanoid")
   if humanoid ~= nil and debounce == false then
   	debounce = true
   	handle.stab:Play()
   	local character = whoTouched.Parent
   	local player = Players:getPlayerFromCharacter(character)
   	local Player = Players:getPlayerFromCharacter(Tool.Parent)
   	if player and (player == Player or IsTeamMate(Player, player) and Tool.CanFriendlyFire.Value == false)then
   		return end
   	if script.Parent.TagsPlayer.Value == true then
   		UntagHumanoid(humanoid)
   		TagHumanoid(humanoid, Player)
   	end
   	
   	local weld = Instance.new("Weld")
   	weld.Parent = character
   	weld.Part0 = hand
   	weld.Part1 = character.HumanoidRootPart
   	humanoid:TakeDamage(40)
   	print("Damage took")
   	this()
   	debounce = false
   	
   end
end
2 Likes
coroutine.wrap(this)

maybe this?

2 Likes

This instantly kills both characters… And also doesn’t work :sad:

2 Likes

you could make the weld as a debris using debris service

game:GetService("DebrisService"):AddItem(weld, 1)

you could make the weld as a debris using debris service

This does not fix my issue as i need others things to happen after the wait such as killing the humanoid and also breaking the weld

Got any answers? This is a core part of gameplay and i really need to make it work

1 Like

You are telling it to return here, which will break the function. The debounce is never set back to false, so the function will no longer work.

But it does, actually. Everything works fine even if i use wait it will just skip that part but everything works fine

thats because the touched function keeps running a lot of times

So, how do i make waiting work?