教學 · 遊戲系統

如何在 Roblox Studio 用 Script 播放動畫

在 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 的動畫是怎麼播放的

這裡有四個物件。你只需要手動建立其中一個,其餘的由 Script 處理。

播放動畫的物件
物件位置作用
Animation由你的 Script 建立存放 AnimationId,也就是動畫素材的位址。它本身不會播放任何東西。
Animator在角色的 Humanoid 裡(由 Roblox 加入)載入動畫,並在骨架的關節上播放。
AnimationTrack由 Animator:LoadAnimation() 回傳可以播放的副本:Play()、Stop()、Looped、Priority、Speed 與事件。
動畫素材在 Roblox 上,從 Animation Editor 發布關鍵影格本身。它的 ID 就是寫進 AnimationId 的值。

每個動畫只載入一次,並把軌道存起來。如果每按一次按鍵就呼叫一次 LoadAnimation,每次都會建立新的軌道,而且會越積越多。

動畫優先順序,從低到高
優先順序用途
Core最低。其他所有動畫都會蓋過它。
Idle站著不動。
Movement走路、跑步、跳躍。
Action表情動作、攻擊、工具。從 Script 播放的動畫通常選這個。
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 Script 裡的標準表情動作,例如 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 會在角色每次出現時複製到角色裡,所以 script.Parent 就是角色,而軌道會在每次重生後重新載入。

當按鍵先被 Roblox 處理時,例如玩家正在聊天中打字,gameProcessed 會是 true。這個檢查可以避免每次有人打出字母 E 時就觸發表情動作。

揮手是所有人都看得到的,而不只是你。只要 Animator 是由伺服器建立的,Roblox 就會複寫玩家在自己角色上播放的動畫,而 Roblox 放進每個角色的 Animator 正是這樣建立的。所以這個 Script 不需要 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)

這個 Script 需要 ReplicatedStorage 中一個名為 PunchHit 的 RemoteEvent,以及你自己帶有 Hit 事件的揮拳動畫,用來取代 rbxassetid://0。

用戶端只負責回報「我的拳打中了」。監聽 PunchHit.OnServerEvent 的伺服器 Script 會決定附近有沒有人、造成多少傷害,並有自己的冷卻時間,讓手動觸發事件的玩家無法隔著整張地圖打人。RemoteEvent 教學示範如何在伺服器上檢查這類請求。

常見錯誤與解法

先開啟 Output 視窗(Window 選單,再選 Output)。大多數動畫問題都會在那裡留下一行訊息。

沒有任何反應,Output 寫著動畫載入失敗

動畫不屬於遊戲擁有者,而是屬於別人。請從 Animation Editor 把你自己的副本發布到擁有這個遊戲的帳號或群組,並使用那個 ID。

動畫有播放,但只有手臂在動,或看起來沒做完

另一個優先順序較高的軌道正在移動同樣的關節。把你的軌道的 Priority 設為 Action(或更高),或在 Animation Editor 中以這個優先順序發布動畫。

沒有任何反應,也沒有錯誤

請檢查骨架類型:R15 動畫在 R6 角色上沒有作用。也請確認 Script 是 StarterCharacterScripts 中的 LocalScript;放在 Workspace 中的 LocalScript 不會執行。

NPC 沒有動畫

它的零件被設為 Anchored,或它的 Humanoid 裡沒有 Animator。只把 HumanoidRootPart 設為 Anchored;步驟 4 的 Script 會在沒有 Animator 時建立一個。

其他玩家看不到我的動畫

動畫是在玩家不擁有的角色上播放,或是在由 LocalScript 建立的 Animator 上播放。自己角色的動畫請從 LocalScript 播放,其他的(NPC、其他玩家)則從伺服器 Script 播放。

用 RoCode 來做

RoCode 是透過外掛程式在 Roblox Studio 中工作的 AI 代理。你描述動作,它就會在你開啟的場景中撰寫 Script。

你輸入

加入按 E 揮手的表情動作,以及按 G 開關的跳舞動作,再讓 NPC 商人對走近的玩家揮手。

RoCode 會做
  • 撰寫 LocalScript 與 NPC 用的 Script,並放到它們該放的位置
  • 可以直接用程式碼組出揮手、揮拳或跳舞這類簡單動作,所以不需要上傳動畫
  • 結束前在 Studio 中檢查寫好的 Script

你仍然需要按下 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 的文件說明的就是它。

來源與延伸閱讀

程式碼如何驗證:本頁每一段 Script 都通過 Luau 嚴格型別檢查,並使用 Roblox API 定義(luau-lsp,2026年11月20日)。型別檢查能找出拼錯的 API 與錯誤的型別,但無法檢查遊戲邏輯,所以發布前請先在 Studio 中測試。

在你自己的遊戲裡試試 RoCode。

每天都有免費點數,不需要信用卡。安裝外掛程式,描述你要的功能,再看看 Studio 裡出現了什麼。