如何在没有显示错误的情况下修复我的战斗脚本?

我创建了这个脚本,希望创建一个可以在一个按钮中播放多个动画的战斗系统。然而,当我将它们放入脚本的轻攻击部分时,动画不会播放,但我的代码中没有错误。

我尝试重新组织,使用实际的动画 ID,更改变量名称等。

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
AnimationId1 = "rbxassetid://2046787868"
AnimationId2 = "rbxassetid://2046881922"
AnimationId3 = "rbxassetid://"
AnimationId4 = "rbxassetid://2048242167"
Debounce = true
local Key = 'U'
local Key2 = 'I'
local Key3 = 'O'
local Key4 = 'P'

local UserInputService = game:GetService("UserInputService")

--轻攻击连击动画。
UserInputService.InputBegan:connect(function(Input, IsTyping)
    for i, v in pairs(game.Players:GetChildren()) do
        if Input.KeyCode == Enum.KeyCode[Key] then
            local Animation = Instance.new("Animation")
            Animation.AnimationId = AnimationId1, AnimationId2
            local LoadAnimation = Humanoid:LoadAnimation(Animation)
            if v == 1 then
                LoadAnimation:Play(AnimationId1)
            elseif v == 2 then
                LoadAnimation:Play(AnimationId2)
            end
        end
    end
end)

--阻挡动画。
UserInputService.InputBegan:connect(function(Input, IsTyping)
    if IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[Key4] and Debounce then
        Debounce = false
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationId4
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:Play()
        wait(.5)
        LoadAnimation:Stop()
        Debounce = true
    end
end)

这个脚本的阻挡部分完美地工作,但当我尝试使用轻攻击部分时,它不起作用。

点赞
用户2860267
用户2860267

在你的轻攻击函数中,v 是一个 Player 对象。所以任何像 v == 1v == 2 这样的检查都会失败,因为它是错误的类型。而且当他们按下 'U' 按钮时,你迭代所有玩家也没有意义。

你可以像你的阻挡动画代码一样制作一个动画。

-- 制作一个计数器来帮助决定要播放哪个动画
local swingCount = 0
local currentSwingAnimation

-- 轻攻击连击序列的动画。
UserInputService.InputBegan:connect(function(Input, IsTyping)
    if IsTyping then return end

    if Input.KeyCode == Enum.KeyCode[Key] then
        swingCount = swingCount + 1

        -- 取消当前播放的任何动画
        if currentSwingAnimation then
            currentSwingAnimation:Stop()
            currentSwingAnimation = nil
        end

        if swingCount == 1 then
            -- 播放第一个动画
            local Animation = Instance.new("Animation")
            Animation.AnimationId = AnimationId1
            currentSwingAnimation = Humanoid:LoadAnimation(Animation)
            currentSwingAnimation.Looped = false
            currentSwingAnimation:Play()

        elseif swingCount == 2 then
            -- 播放第二个动画
            local Animation = Instance.new("Animation")
            Animation.AnimationId = AnimationId2
            currentSwingAnimation = Humanoid:LoadAnimation(Animation)
            currentSwingAnimation.Looped = false
            currentSwingAnimation:Play()

            -- 重置摆动计数器以开始新的连击序列
            swingCount = 0
        end
    end
end)
2019-06-25 23:22:45