首页 > 解决方案 > 没有显示错误时如何修复我的战斗脚本?

问题描述

我创建了这个脚本,希望创建一个可以一键播放多个动画的战斗系统;但是,当我将它们放在脚本的轻攻击部分时,动画将无法播放,但我的代码中没有错误。

我尝试过重新组织,使用实际的动画 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")

--Animation for the Light attk combo sequence.
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)



--Animation for the Blocking sequence.
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)

该脚本的阻止部分完美运行,但是,当我尝试使用轻攻击部分时,它不起作用。

标签: luaroblox

解决方案


在你的轻攻击函数中,v是一个Player对象。所以任何检查都喜欢v == 1v == 2将失败,因为它是错误的类型。当他们按下“U”按钮时,你会遍历所有玩家,这也没有任何意义。

您可以让它播放动画,就像使用阻塞动画代码一样。

-- make a counter to help decide which animation to play
local swingCount = 0
local currentSwingAnimation    

--Animation for the Light attack combo sequence.
UserInputService.InputBegan:connect(function(Input, IsTyping)
    if IsTyping then return end

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

        -- cancel any animation currently playing
        if currentSwingAnimation then
            currentSwingAnimation:Stop()
            currentSwingAnimation = nil
        end

        if swingCount == 1 then
            -- play the first animation
            local Animation = Instance.new("Animation")
            Animation.AnimationId = AnimationId1
            currentSwingAnimation = Humanoid:LoadAnimation(Animation)
            currentSwingAnimation.Looped = false
            currentSwingAnimation:Play()

        elseif swingCount == 2 then
            -- play the second swing animation
            local Animation = Instance.new("Animation")
            Animation.AnimationId = AnimationId2
            currentSwingAnimation = Humanoid:LoadAnimation(Animation)
            currentSwingAnimation.Looped = false
            currentSwingAnimation:Play()

            -- reset the swing counter to start the combo over
            swingCount = 0
        end
    end
end)

推荐阅读