lua - 没有显示错误时如何修复我的战斗脚本?
问题描述
我创建了这个脚本,希望创建一个可以一键播放多个动画的战斗系统;但是,当我将它们放在脚本的轻攻击部分时,动画将无法播放,但我的代码中没有错误。
我尝试过重新组织,使用实际的动画 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)
该脚本的阻止部分完美运行,但是,当我尝试使用轻攻击部分时,它不起作用。
解决方案
在你的轻攻击函数中,v
是一个Player对象。所以任何检查都喜欢v == 1
或v == 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)
推荐阅读
- java - 如何使用 Spring Security 和 RESTful 进行 POST 请求?
- excel - 根据单元格值将行从两个或多个 Excel 工作表复制到另一个工作表
- laravel-query-builder - 将 Zend 查询转换为 laravel 并有条件地使用 when,然后按 order by
- symfony - Symfony autowire:为什么服务注入为空?
- c# - 删除 DateTime.Now C# 中所有周末的扩展方法
- r - xaringan::summon_remark() 没有来自 R 的互联网连接
- java - Apache Kylin Cube Building 失败(使用 mysql JDBC 连接器时)
- excel - VBA:写入单元格时代码停止执行
- python - 对象检测算法/神经网络输出什么?
- css - 如何实现移动分屏,但宽度是屏幕尺寸的两倍?