How can i make it so my tool detects whe it's activated because it's not working

50 Views Asked by At

My roblox tool has a function to perform when activated but it literally does nothing

I tried to change it to tool.Deactivated, no change. I added print functions to my function to check if it was working, none of the prints worked, and theres no errors or anything.

If you want the code, here it is:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local tool = script.Parent

local leftPunch = animator:LoadAnimation(script:WaitForChild("RookieLeftPunch"))
local rightPunch = animator:LoadAnimation(script:WaitForChild("RookieRightPunch"))

local currentPunch = 0

local debounce = false 
tool.Equipped:Connect(function()
    print("equipped")
    tool.Activated:Connect(function()
        if debounce then return end

        debounce = true
        if currentPunch == 0 then
            rightPunch:Play()
            task.wait(0.4)
            print("punched 1")
            debounce = false
        elseif currentPunch == 1 then
            leftPunch:Play()
            task.wait(0.4)
            print("punched 2")
            debounce = false
        elseif currentPunch == 2 then
            rightPunch:Play()
            task.wait(0.8)
            print("punched 3")
            debounce = false
        end

        if currentPunch == 2 then
            currentPunch = 0
        else
            currentPunch += 1
        end
    end)


end)


1

There are 1 best solutions below

0
Devallex On

Usually, when there are no errors or print messages, it means a few things:

  1. The script didn't run because it was in a context where It couldn't (This is unlikely here but you can always make sure it isn't the problem)
  2. Wait for child or an event got stuck on something because it doesn't exist. (Try waiting ~10 ish seconds you might see an orange warning message in the console)

Here is the revised code, with comments, which may help you find the problem.

print("The script ran!") -- Use this to figure out if the script is in a context where it can run, it probably can.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

print("Animator was loaded!") -- Use this to know that :WaitForChild() didn't get stuck looking for character/humanoid/animator

local tool = script.Parent

local leftPunch = animator:LoadAnimation(script:WaitForChild("RookieLeftPunch"))
local rightPunch = animator:LoadAnimation(script:WaitForChild("RookieRightPunch"))

print("Animations were loaded!") -- Use this to know that :WaitForChild() didn't get stuck looking for your animations

local currentPunch = 0

local debounce = false 

-- Using tool.equipped would be unreliable, as it would keep :Connecting tool.Activated every time it is equipped, which is not a good practice.
-- Tool.activated only runs when the tool is equipped anyway, so it's more efficient
tool.Activated:Connect(function()
    if debounce then return end

    debounce = true

    print("Current punch" .. currentPunch) -- More efficient way to debug and tells you if it's not 1, 2 or 3
    if currentPunch == 0 then
        rightPunch:Play()
        task.wait(0.4)
    elseif currentPunch == 1 then
        leftPunch:Play()
        task.wait(0.4)
    elseif currentPunch == 2 then
        rightPunch:Play()
        task.wait(0.8)
    end

    if currentPunch == 2 then
        currentPunch = 0
    else
        currentPunch += 1
    end
    
    -- Safer and more efficient to put a single debounce = false at the end, in case currentPunch isn't 1, 2, or 3 for some reason
    debounce = false
end)