教程 · 游戏系统

如何在 Roblox Studio 里用脚本播放动画

在 Roblox 中播放动画的是角色的 Animator,而不是动画本身。在这篇教程里,我们会在玩家按下按键时播放动画,开关一段舞蹈,在 NPC 身上循环播放动画,并在拳头击中目标的那一帧造成伤害。

更新于 2026年11月20日阅读约 3 分钟作者

要点

把 LocalScript 放进 StarterPlayer > StarterCharacterScripts。在脚本里找到角色的 Humanoid 和它的 Animator,用你自己的 AnimationId 创建一个 Animation,通过 animator:LoadAnimation(animation) 加载它,然后在 UserInputService.InputBegan 报告你的按键时,对得到的轨道调用 :Play()。玩家这样在自己角色上播放的动画,服务器上的所有人都能看到。

  • 动画必须属于游戏的所有者(你或你的群组)或 Roblox。别人上传的动画在你的游戏里不会播放。
  • 把轨道的 Priority 设为 Action,否则行走和站立动画可能会盖过它。
  • 用 Animator:LoadAnimation 加载动画。Humanoid:LoadAnimation 已经弃用。
本页内容
  1. 工作方式
  2. 1. 动画 ID
  3. 2. 按键播放
  4. 3. 循环舞蹈
  5. 4. NPC 动画
  6. 5. 动画事件
  7. Common errors
  8. 用 RoCode
  9. 常见问题
  10. 参考资料

Roblox 中动画是怎么播放的

一共涉及四个对象。你手动创建的只有其中一个,其余的由脚本完成。

播放动画的对象
对象位置作用
Animation由你的脚本创建保存 AnimationId,即动画资源的地址。它本身不播放任何东西。
Animator在角色的 Humanoid 里面(由 Roblox 添加)加载动画,并在骨架的关节上播放。
AnimationTrack由 Animator:LoadAnimation() 返回可以播放的副本:Play()、Stop()、Looped、Priority、Speed 和各种事件。
动画资源在 Roblox 上,从 Animation Editor 发布关键帧本身。它的 ID 就是写进 AnimationId 的内容。

每个动画只加载一次,并保存好轨道。如果每次按键都调用 LoadAnimation,每次都会创建一条新轨道,它们会越积越多。

动画优先级,从低到高
优先级用途
Core最低。其他所有优先级都会盖过它。
Idle原地站立。
Movement行走、奔跑、跳跃。
Action表情动作、攻击、工具。从脚本播放的动画通常选它。
Action2 到 Action4需要盖过另一个动作的动作,例如在攻击之上的眩晕。

当两条正在播放的轨道移动同一个关节时,在这个关节上优先级更高的轨道获胜。一个只移动手臂的 Action 挥手动画,不会影响双腿继续行走。

第 1 步:找到可以使用的动画 ID

只有当动画属于游戏所有者或 Roblox 时,Roblox 才会在你的游戏里播放它。所以从 Creator Store 或视频里复制的大多数 ID 都用不了:它们属于别人,无法加载。有两个可靠的来源。

  1. 自己做一个。在 Studio 中打开 Avatar 选项卡,选择 Animation Editor,选中一个骨架(同一选项卡上的 Rig Builder 可以插入骨架),在时间轴上摆好姿势,然后在编辑器菜单中选择 Publish to Roblox。请发布到拥有这个游戏的同一个账户或群组。复制你得到的 ID。
  2. 使用 Roblox 的动画。Roblox Animate 脚本里的标准表情动作,例如 R15 挥手(507770239)和跳舞(507771019),属于 Roblox,所以在任何游戏里都能用。本教程用的就是它们,方便你马上测试。

ID 的写法是 "rbxassetid://" 加上后面的数字,例如 "rbxassetid://507770239"。

第 2 步:按下按键时播放动画

  1. 在 Explorer 中展开 StarterPlayer,把鼠标移到 StarterCharacterScripts 上,点击 +,插入一个 LocalScript。
  2. 把它重命名为 EmoteOnKey,并粘贴下面的代码。
  3. 点击 Play,然后按 E。你的角色会挥手。
LocalScript 位置: StarterCharacterScripts 名称: EmoteOnKey
-- StarterPlayer > StarterCharacterScripts 中名为 EmoteOnKey 的 LocalScript。
-- 按 E 在自己的角色上播放动画。服务器上的所有人都能看到。
local UserInputService = game:GetService("UserInputService")

local ANIMATION_ID = "rbxassetid://507770239" -- Roblox 官方的 R15 挥手动画;在这里填入你自己的动画 ID
local KEY = Enum.KeyCode.E
local COOLDOWN_SECONDS = 1

local character = script.Parent :: Model
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
local animator = humanoid:WaitForChild("Animator") :: Animator

local animation = Instance.new("Animation")
animation.AnimationId = ANIMATION_ID
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Action -- 高于行走和站立,所以双腿继续走,双手挥动

local lastPlayed = -math.huge

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessed: boolean)
	-- 玩家在聊天框或文本框里打字时,gameProcessed 为 true
	if gameProcessed or input.KeyCode ~= KEY then
		return
	end
	if humanoid.Health <= 0 or os.clock() - lastPlayed < COOLDOWN_SECONDS then
		return
	end
	lastPlayed = os.clock()
	track:Play(0.1) -- 0.1 秒内平滑淡入
end)

StarterCharacterScripts 中的脚本会在角色每次出现时复制到角色里,所以 script.Parent 就是角色,每次重生后轨道都会重新加载。

当按键先被 Roblox 处理时,gameProcessed 为 true,例如玩家正在聊天框里打字。这个检查能防止每当有人输入字母 E 时表情动作就被触发。

挥手动作所有人都能看到,而不只是你。如果 Animator 是由服务器创建的,Roblox 会复制玩家在自己角色上播放的动画,而 Roblox 放进每个角色的 Animator 正是这样创建的。所以这个脚本不需要 RemoteEvent。

第 3 步:开关循环播放的动画

跳舞或坐下应该一直持续,直到玩家停止。把 Looped 设为 true,每次按键都启动或停止同一条轨道。这个版本还会在玩家开始走动时停止跳舞。

LocalScript 位置: StarterCharacterScripts 名称: DanceToggle
-- StarterPlayer > StarterCharacterScripts 中名为 DanceToggle 的 LocalScript。
-- 按 G 开始循环跳舞,再按一次 G(或走动)停止。
local UserInputService = game:GetService("UserInputService")

local DANCE_ID = "rbxassetid://507771019" -- Roblox 官方的 R15 跳舞动画;在这里填入你自己的动画 ID
local KEY = Enum.KeyCode.G

local character = script.Parent :: Model
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
local animator = humanoid:WaitForChild("Animator") :: Animator

local animation = Instance.new("Animation")
animation.AnimationId = DANCE_ID
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Action
track.Looped = true

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessed: boolean)
	if gameProcessed or input.KeyCode ~= KEY then
		return
	end
	if track.IsPlaying then
		track:Stop(0.2)
	else
		track:Play(0.2)
	end
end)

-- 玩家开始走动时停止跳舞
humanoid.Running:Connect(function(speed: number)
	if speed > 0.5 and track.IsPlaying then
		track:Stop(0.2)
	end
end)

传给 Play 和 Stop 的数字是平滑过渡的时间,单位为秒。短暂的过渡让舞蹈平滑地开始和结束,而不是让角色从一个姿势猛地跳到另一个姿势。

第 4 步:在 NPC 身上播放动画

NPC 不属于任何玩家,所以它的动画由服务器播放。把一个 Script(不是 LocalScript)放进 NPC 模型里。用 Rig Builder 做的骨架已经有 Humanoid、HumanoidRootPart 和 Animator。

Script 位置: NPC 模型 名称: NpcAnimation
-- NPC 模型里名为 NpcAnimation 的 Script。NPC 需要有 Humanoid 和 HumanoidRootPart。
-- 从服务器在 NPC 上循环播放动画,所以每个玩家都能看到。
local ANIMATION_ID = "rbxassetid://507771019" -- Roblox 官方的 R15 跳舞动画;在这里填入你自己的动画 ID

local npc = script.Parent :: Model
local humanoid = npc:WaitForChild("Humanoid") :: Humanoid

-- 由骨架做成的 NPC 通常已经有 Animator;如果没有,就创建一个
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
	local newAnimator = Instance.new("Animator")
	newAnimator.Parent = humanoid
	animator = newAnimator
end
assert(animator, "the NPC has no Animator")

local animation = Instance.new("Animation")
animation.AnimationId = ANIMATION_ID
local track = animator:LoadAnimation(animation)
track.Looped = true
track:Play()

第 5 步:把动作绑定到动画中的某个时刻

出拳应该在拳头击中目标时造成伤害,而不是在按下按键时。在 Animation Editor 中,在这个时刻右键点击时间轴,添加一个名为 Hit 的 animation event,然后重新发布动画。在代码里,track:GetMarkerReachedSignal("Hit") 会在这一帧触发。

LocalScript 位置: StarterCharacterScripts 名称: PunchOnClick
-- StarterPlayer > StarterCharacterScripts 中名为 PunchOnClick 的 LocalScript。
-- 点击时播放你的出拳动画,并在拳头击中目标的时刻通知服务器,
-- 通过你在 Animation Editor 中添加的动画事件 "Hit"。
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local PUNCH_ID = "rbxassetid://0" -- 你的出拳动画,在击中时刻带有 "Hit" 事件
local COOLDOWN_SECONDS = 0.6

local punchHit = ReplicatedStorage:WaitForChild("PunchHit") :: RemoteEvent
local character = script.Parent :: Model
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
local animator = humanoid:WaitForChild("Animator") :: Animator

local animation = Instance.new("Animation")
animation.AnimationId = PUNCH_ID
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Action

track:GetMarkerReachedSignal("Hit"):Connect(function()
	punchHit:FireServer() -- 服务器在造成伤害前检查距离和冷却时间
end)

local lastPunch = -math.huge
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessed: boolean)
	if gameProcessed or input.UserInputType ~= Enum.UserInputType.MouseButton1 then
		return
	end
	if os.clock() - lastPunch < COOLDOWN_SECONDS then
		return
	end
	lastPunch = os.clock()
	track:Play(0.05)
end)

这个脚本需要 ReplicatedStorage 中一个名为 PunchHit 的 RemoteEvent,以及你自己的带 Hit 事件的出拳动画,用来替换 rbxassetid://0。

客户端只负责报告“我的拳头打中了”。监听 PunchHit.OnServerEvent 的服务端 Script 决定附近是否有人、造成多少伤害,并有自己的冷却时间,这样手动触发事件的玩家就无法隔着整张地图打人。RemoteEvent 教程展示了如何在服务器上验证这样的请求。

常见错误与解决办法

先打开 Output 窗口(Window 菜单,然后选 Output)。大多数动画问题都会在那里留下一行信息。

什么都没发生,Output 显示动画加载失败

这个动画不属于游戏所有者,而属于别人。从 Animation Editor 把你自己的副本发布到拥有这个游戏的账户或群组,并使用这个 ID。

动画在播放,但只有手臂在动,或者看起来不完整

另一条优先级更高的轨道在移动同样的关节。把你的轨道的 Priority 设为 Action(或更高),或者在 Animation Editor 中以这个优先级发布动画。

什么都没发生,也没有错误

检查骨架类型:R15 动画在 R6 角色上没有任何效果。同时确认脚本是 StarterCharacterScripts 里的 LocalScript;放在 Workspace 里的 LocalScript 不会运行。

NPC 不动

它的零件被锚固了,或者它的 Humanoid 里没有 Animator。只锚固 HumanoidRootPart;第 4 步的脚本会在没有 Animator 时创建一个。

其他玩家看不到我的动画

动画是在玩家不拥有的角色上播放的,或者是在 LocalScript 创建的 Animator 上播放的。自己角色的动画从 LocalScript 播放,其他一切(NPC、其他玩家)从服务端 Script 播放。

用 RoCode 来做

RoCode 是通过插件在 Roblox Studio 里工作的 AI 代理。你描述想要的动作,它会在你打开的场景里编写脚本。

你输入

加一个按 E 挥手的表情动作,和一个我可以按 G 开关的舞蹈,再让 NPC 商人向走近他的玩家挥手。

RoCode 的操作
  • 编写 LocalScript 和 NPC 用的 Script,并把它们放到应该放的位置
  • 可以直接在代码里组合出挥手、出拳或跳舞这样的简单动作,所以不需要上传动画
  • 在结束前于 Studio 中检查它写的脚本

你仍然需要点击 Play,确认动作看起来是你想要的样子。RoCode 如何连接到 Studio。

常见问题

我能使用自己找到的任何动画 ID 吗?

不能。动画只会在其所有者拥有的游戏里播放,或者在它属于 Roblox 时播放。请使用你自己的动画或 Roblox 的标准动画。

其他玩家能看到 LocalScript 播放的动画吗?

能,前提是玩家通过 Roblox 给角色的 Animator 在自己的角色上播放它。NPC 身上的动画需要从服务端 Script 播放。

Roblox 里怎么让动画循环播放?

在播放前设置 track.Looped = true,或者在发布前在 Animation Editor 中打开 Loop。

怎么停止动画?

对你启动的轨道调用 track:Stop()。传入一个数字,例如 track:Stop(0.2),就能让它在这么多秒内平滑淡出。

为什么用 Animator,而不是 Humanoid:LoadAnimation?

Humanoid:LoadAnimation 已经弃用。Animator:LoadAnimation 做的是同样的事,Roblox 文档描述的也正是它。

参考资料

代码如何验证:本页的所有脚本都已用 Roblox API 类型定义(luau-lsp,2026年11月20日)通过 Luau strict 类型检查。类型检查能发现 API 名称写错和类型不匹配,但发现不了游戏逻辑上的错误。发布前请务必在 Studio 中测试。

在你自己的游戏里试试 RoCode。

每天有免费额度,无需绑定银行卡。安装插件,描述你想要的功能,然后看看它在 Studio 里做了什么。