我怎样才能让我的工具检测到它何时被激活,因为它不工作

问题描述 投票:0回答:1

我的 roblox 工具在激活时有一个执行的功能,但它实际上什么也没做

我尝试将其更改为tool.Deactivated,没有变化。我在函数中添加了打印函数来检查它是否正常工作,没有任何打印工作正常,并且没有错误或任何错误。

如果您想要代码,这里是:

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)


lua roblox luau
1个回答
0
投票

通常,当没有错误或打印消息时,这意味着以下几点:

  1. 脚本未运行,因为它处于无法运行的上下文中(这在这里不太可能,但您始终可以确保这不是问题)
  2. 等待子项或事件因某物不存在而卡住。 (尝试等待大约 10 秒,您可能会在控制台中看到橙色警告消息)

这里是修改后的代码,有注释,可以帮助你找到问题。

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)
© www.soinside.com 2019 - 2024. All rights reserved.