How do I get a part every time with .touched?

Hello developers. I was working on a pickaxe system when I got a problem I couldn’t solve. I wanted to make it so every time you click, the pickaxe will check what its touching, then print it in the output. It works the first time, then it doesn’t work again. Its probably a really simple fix, but I can’t find it.

Here’s the script:

local player = game:GetService("Players").LocalPlayer
local character = player.CharacterAdded:Wait()
local userInputService = game:GetService("UserInputService")
local pickaxeTool = script.Parent
local damagePart = pickaxeTool:WaitForChild("DamagePart")
local animation  = Instance.new("Animation")
local debounce = false
animation.AnimationId = "rbxassetid://13885125698"

repeat wait() until game

local track
track = character:WaitForChild("Humanoid"):LoadAnimation(animation)

local function mine(hit)
	if debounce == false then
		print(hit)
		debounce = true
	end
end

local function mineAnimation(input,_gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if track.IsPlaying == false then
			if character:FindFirstChild(pickaxeTool.Name) then
				track.Priority = Enum.AnimationPriority.Action
				track.Looped = false
				track:Play()
				debounce = false
				damagePart.Touched:Connect(mine)
			end
		end
	end
end

userInputService.InputBegan:Connect(mineAnimation)

Thank you for the help!

5 Likes

You have to set debounce back to false

local function mine(hit)
	if debounce == false then
		print(hit)
		debounce = true
		task.wait(1)
		debounce = false
	end
end

It works, but then you don’t have to click to get the hit printed

just remove the debounce = false line in the mineAnimation function

Yeah I already had tried that and it still didnt work.

can u explain how the you don’t have to click to get the hit printed?

When you first click it prints but then you can just walk and without clicking parts will get printed that the pickaxe touches

does that never stop or it does stop after a bit?

Once you click then you can always get parts printed without clicking

Try ti use:Once instead of :Connect on the touched event

It didn’t work. You could only click :Once then it wouldnt print after that

I think the problem is that the .Touched isn’t an if statement.

I wouldn’t recommend using .Touched(). I would recommend using workspace:GetPartsinPart().

Just keep in mind that it will print EVERY part that it is inside of. So, if the character is touching the damagePart then this will also print out parts of the character.

local function mineAnimation(input,_gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if track.IsPlaying == false then
			if character:FindFirstChild(pickaxeTool.Name) then
				track.Priority = Enum.AnimationPriority.Action
				track.Looped = false
				track:Play()
				debounce = false
				local parts = workspace:GetPartsInPart(damagePart)
				for i, v in pairs(parts) do
					print(v.Name)
				end
			end
		end
	end
end

You could also use :GetTouchingParts() and do something similar to this if this doesn’t work.

local player = game:GetService("Players").LocalPlayer
local character = player.CharacterAdded:Wait()
local userInputService = game:GetService("UserInputService")
local pickaxeTool = script.Parent
local damagePart = pickaxeTool:WaitForChild("DamagePart")
local animation  = Instance.new("Animation")
local debounce = false
animation.AnimationId = "rbxassetid://13885125698"

repeat wait() until game

local track
track = character:WaitForChild("Humanoid"):LoadAnimation(animation)

local function mine(hit)
	if debounce == false then
		print(hit)
		debounce = true
	end
end

local function mineAnimation(input,_gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if track.IsPlaying == false then
			if character:FindFirstChild(pickaxeTool.Name) then
				track.Priority = Enum.AnimationPriority.Action
				track.Looped = false
				track:Play()
				debounce = false
				damagePart.Touched:Connect(function(mine) --here
			end
		end
	end
end

userInputService.InputBegan:Connect(mineAnimation)

This might work.