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)
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)