我创建了这个脚本,希望创建一个可以在一个按钮中播放多个动画的战斗系统。但是,当我将它们放在脚本的轻击部分时,动画将无法播放,但是我的代码中没有错误。
我尝试使用实际的动画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)
此脚本的阻止部分可以完美地工作,但是,当我尝试使用轻度攻击部分时,该部分将无法工作。
答案 0 :(得分:1)
在您的轻击功能中,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)