You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox中NPC播放idle动画异常:Animation2无法结束如何修复?

问题根源

你当前的脚本依赖KeyframeReached事件触发下一次动画切换,但Roblox默认的Idle动画(尤其是Animation2)可能没有设置额外的关键帧,导致这个事件永远不会触发,Animation2会一直循环播放无法切换。另外,循环动画的Ended事件不会自动触发(除非手动停止),这也是你之前添加该事件无效的原因。

修复方案

根据Idle动画的使用场景,提供两种可行的修复方式:

方式一:动画播放一次后自动切换

适合需要严格控制单次播放后切换的场景,需修改动画循环模式:

local idle1 = script.idle.Animation1
local idle2 = script.idle.Animation2

local animator = script.Parent.Humanoid.Animator

local LoadedIdle1 = animator:LoadAnimation(idle1)
local LoadedIdle2 = animator:LoadAnimation(idle2)

-- 将动画设置为单次播放模式
LoadedIdle1.LoopP = false
LoadedIdle2.LoopP = false

local currentAnimation = nil

local function PlayAnimation()
    -- 停止当前正在播放的动画,避免叠加冲突
    if currentAnimation and currentAnimation.IsPlaying then
        currentAnimation:Stop()
    end

    local roll = math.random(1, 10)
    local animation = roll == 1 and LoadedIdle2 or LoadedIdle1

    currentAnimation = animation
    animation:Play()

    -- 绑定Ended事件,触发一次后立即断开,防止内存泄漏
    local connection
    connection = animation.Ended:Connect(function()
        connection:Disconnect()
        PlayAnimation()
    end)
end

PlayAnimation()

方式二:循环播放+随机时间切换

更符合NPC Idle动画的自然表现,保持动画循环,每隔随机时长切换:

local idle1 = script.idle.Animation1
local idle2 = script.idle.Animation2

local animator = script.Parent.Humanoid.Animator

local LoadedIdle1 = animator:LoadAnimation(idle1)
local LoadedIdle2 = animator:LoadAnimation(idle2)

-- 显式设置为循环播放(默认可能已开启,确保一致性)
LoadedIdle1.LoopP = true
LoadedIdle2.LoopP = true

local currentAnimation = nil
-- 切换动画的随机时间范围(单位:秒),可根据需求调整
local switchDelayRange = {5, 15}

local function PlayAnimation()
    -- 停止当前动画,避免冲突
    if currentAnimation and currentAnimation.IsPlaying then
        currentAnimation:Stop()
    end

    local roll = math.random(1, 10)
    local animation = roll == 1 and LoadedIdle2 or LoadedIdle1

    currentAnimation = animation
    animation:Play()

    -- 随机延迟后切换下一个动画
    local delayTime = math.random(switchDelayRange[1], switchDelayRange[2])
    task.wait(delayTime)
    PlayAnimation()
end

PlayAnimation()

内容的提问来源于stack exchange,提问作者Sebwazhere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 12:45:33