Why does my function stop at Wait()

I am using a tool that enables this script when an attack is launched. It works fine, but when I try to add a wait() the function just stops. The script will work fine and will continue working, but the function basically stops at wait().

I tried using task.wait(), nothing. I tried firing a function that had wait in it instead of just doing wait, didn’t work.

The script:

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
1 Like

Maybe the function has not run yet, put another print inside the touched function

2 Likes

Try this and let me know if it works

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()
   	task.wait(1) -- Wait here
   	weld:Destroy() -- Destroy the weld after waiting
   	debounce = false -- Reset debounce here
   end
end
1 Like

I tried this but sadly the script didn’t work.

1 Like

I will see if I can find another solution for you.

Im so desperate, this is my second post and no ones seems to figure out, its a core part of gameplay

Does humanoid take damage? Just so we know

Yes since its before the function/wait()

what happens if you put the function before?

Why do use a seperate function for the debounce/wait, wouldn’t it be smarter to do this:

local debounce = false -- define it

function touched(whoTouched)
    if debounce then
        return
    end
    
    debounce = true
    
    local humanoid = whoTouched.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid ~= nil then
        -- your logic here
        
        wait(1) -- instead of this()
        
        debounce = false
    end
end
1 Like

Everything below it doesnt work.

maybe do

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

Once again this script you provided doesnt work

This still doesnt work. If it can help this script is originally disabled and is enabled with this code:


```local tool = script.Parent
local handle = tool.Handle
enabled = true
local function heal()
	if enabled == false 
	then return end
	enabled = false
	local animation = script:FindFirstChild('Animation')
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	local dance = humanoid:LoadAnimation(animation)
	dance:Play()
		handle.swing:Play()
	tool.Stab.Disabled = false
	task.wait(script.Parent.SwingDelay.Value)
	tool.Stab.Disabled = true
	enabled = true
end
tool.Activated:Connect(heal)

is the function running at all? put a print before the wait

It is. I stated in the description that it worked.

try using task.spawn(this) instead

You could be disabling the script too early.

I think that was the issue my bad to this whole comment section :man_facepalming:

This is my script now but problem is since it doesnt wait for the function to finish it instantly destroys the weld

local 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

local function this()
	task.wait(3)
	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(1)
		print("Damage took")
		task.spawn(this)-- Wait here Destroy the weld after waiting
		weld:Destroy()
		debounce = false -- Reset debounce here
	end
end

function Equipped()
	local Character = Tool.Parent
	Player = Players:getPlayerFromCharacter(Character)
end

handle.Touched:Connect(touched)