Help with event inside event

hello,

I was working on an axe and I wanted the script to check if the axe collides with anything while it’s activated, however, when I activate it the tip.Touched event still runs even if it is already done

am I doing something wrong?

local tool = script.Parent
local tip = tool.Part

local check = false

local players = game:GetService("Players")

tool.Activated:Connect(function()
	local player =	players:GetPlayerFromCharacter(tool.Parent)
	tip.Touched:Connect(function(part)
		print(part)
		if part == "Tree" then
			print(1)
		else
			print(2)	
		end
	end)	
		end)	

thank you

Something like this should work let me know.

local tool = script.Parent
local tip = tool.Part

local check = false

local players = game:GetService("Players")

local canBeTouched = true --// This is the debounce.

tool.Activated:Connect(function()
	local player =	players:GetPlayerFromCharacter(tool.Parent)
	tip.Touched:Connect(function(part)
		if (canBeTouched) then
			canBeTouched = false
			print(part)
			if part == "Tree" then
				print(1)
			else
				print(2)	
			end
			wait(2) --// The Time Per Hit.
			canBeTouched = true
		end
	end)	
end)

Avoid putting events inside event blocks, it will lead to problems like the event firing multiple times. Consider trying checks instead.

1 Like

debounce is exactly what i needed, my thanks