如何停止动画循环?

嗨,我正在制作一个塔防游戏,但是塔的攻击动画一直循环。 这里是代码(不是整个代码)

function Tower.Attack(newTower)
    local target = FindNearestTarget(newTower)
    if target then
animateTowerEvent:FireAllClients(newTower, "Attack")
target.Humanoid:TakeDamage(40)
end

task.wait(1)

Tower.Attack(newTower)
end

以下是动画脚本:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local animateTowerEvent = events:WaitForChild("AnimateTower")

local function SetAnimation(object, animName)
    local humanoid = object:WaitForChild("Humanoid")
    local animationsFolder = object:WaitForChild("Animations")
    if humanoid and animationsFolder then
        local animationObject = animationsFolder:WaitForChild(animName)
        if animationObject then
            local animator = humanoid:FindFirstChild("Animator")
            local animationTrack = animator:LoadAnimation(animationObject)
            return animationTrack
        end
    end
end
local function playAnimation(object, animName)
    local animationTrack = SetAnimation(object, animName)
    if animationTrack then
        animationTrack:Play()
    else
        warn("Animation track does not exist")
        return
    end
end

workspace.Mobs.ChildAdded:Connect(function(object)
    playAnimation(object, "Walk")
workspace.Towers.ChildAdded:Connect(function(object)
    playAnimation(object, "Idle")
end)
animateTowerEvent.OnClientEvent:Connect(function(tower, animName)
    playAnimation(tower, animName)
end)

我真的希望这个本地脚本能帮助到你

原文链接 https://stackoverflow.com/questions/70716722

点赞
stackoverflow用户15818349
stackoverflow用户15818349

在创建 AnimationTrack 动画轨迹时,您可能在动画编辑器中将 Looped 属性设置为 true。 您可以通过以下两种方式之一防止动画循环:

  1. 在动画编辑器中编辑 Looped 属性并更新动画。
  2. SetAnimation 函数中将 Looped 属性设置为 false
local function SetAnimation(object, animName)
    local humanoid = object:WaitForChild("Humanoid")
    local animationsFolder = object:WaitForChild("Animations")
    if humanoid and animationsFolder then
        local animationObject = animationsFolder:WaitForChild(animName)
        if animationObject then
            local animator = humanoid:FindFirstChild("Animator")
            local animationTrack = animator:LoadAnimation(animationObject)
            animationTrack.Looped = false
            return animationTrack
        end
    end
end
2022-01-14 22:16:06