Ghi chú phát hành
Học
Lớp công cụ
AnimationTrack
Không Thể Tạo

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.


Tóm Tắt
Phương Pháp
AdjustSpeed(speed: number):()
AdjustWeight(weight: number,fadeTime: number):()
GetParameter(key: string):Variant
Play(fadeTime: number,weight: number,speed: number):()
SetParameter(key: string,value: Variant):()
SetTargetInstance(name: string,target: Instance?):()
Stop(fadeTime: number):()
Thành viên kế thừa

Tài Liệu Tham Khảo API
Thuộc Tính
Animation
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: Animation
AnimationTrack.Animation:Animation
Mẫu mã
Lắng Nghe Các Hoạt Hình Mới
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
animator.AnimationPlayed:Connect(function(animationTrack)
local animationName = animationTrack.Animation.Name
print("Hoạt hình đang phát " .. animationName)
end)

IsPlaying
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack.IsPlaying:boolean
Mẫu mã
AnimationTrack ĐangChạy
local function playOrAdjust(animationTrack, fadeTime, weight, speed)
if not animationTrack.IsPlaying then
animationTrack:Play(fadeTime, weight, speed)
else
animationTrack:AdjustSpeed(speed)
animationTrack:AdjustWeight(weight, fadeTime)
end
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playOrAdjust(animationTrack, 1, 0.6, 1)

Length
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: Animation
AnimationTrack.Length:number
Mẫu mã
Chơi hoạt hình trong một khoảng thời gian cụ thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)

Looped
Đọc Song Song
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack.Looped:boolean
Mẫu mã
Vòng Lặp Hoạt Hình
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
local animationTrack = animator:LoadAnimation(animation)
animationTrack.Looped = false
task.wait(3)
animationTrack:Play()
task.wait(4)
animationTrack.Looped = true
animationTrack:Play()
Chạy AnimationTrack trong một số vòng lặp
local function playForNumberLoops(animationTrack, number)
animationTrack.Looped = true
animationTrack:Play()
local numberOfLoops = 0
local connection = nil
connection = animationTrack.DidLoop:Connect(function()
numberOfLoops = numberOfLoops + 1
print("vòng lặp: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- quan trọng để ngắt kết nối khi không còn cần thiết
end
end)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playForNumberLoops(animationTrack, 5)

Priority
Đọc Song Song
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack.Priority:Enum.AnimationPriority

Speed
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack.Speed:number
Mẫu mã
Tốc độ hoạt hình
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
ContentProvider:PreloadAsync({ animation })
local animationTrack = animator:LoadAnimation(animation)
local normalSpeedTime = animationTrack.Length / animationTrack.Speed
animationTrack:AdjustSpeed(3)
local fastSpeedTime = animationTrack.Length / animationTrack.Speed
print("Với tốc độ bình thường, hoạt hình sẽ phát trong", normalSpeedTime, "giây")
print("Với tốc độ gấp 3 lần, hoạt hình sẽ phát trong", fastSpeedTime, "giây")
Chơi hoạt hình trong một khoảng thời gian cụ thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)

TimePosition
Không Sao Chép
Đọc Song Song
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack.TimePosition:number
Mẫu mã
Đóng băng hoạt ảnh tại vị trí
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
freezeAnimationAtTime(animationTrack, 0.5)
freezeAnimationAtPercent(animationTrack, 50)

WeightCurrent
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack.WeightCurrent:number
Mẫu mã
WeightCurrent và WeightTarget
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- thời gian chờ tùy ý để cho phép nhân vật rơi vào vị trí
local animationTrack1 = animator:LoadAnimation(animation1)
local animationTrack2 = animator:LoadAnimation(animation2)
animationTrack1.Priority = Enum.AnimationPriority.Movement
animationTrack2.Priority = Enum.AnimationPriority.Action
animationTrack1:Play(0.1, 5, 1)
animationTrack2:Play(10, 3, 1)
local done = false
while not done and task.wait(0.1) do
if math.abs(animationTrack2.WeightCurrent - animationTrack2.WeightTarget) < 0.001 then
print("đã đến nơi")
done = true
end
end

WeightTarget
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack.WeightTarget:number
Mẫu mã
WeightCurrent và WeightTarget
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- thời gian chờ tùy ý để cho phép nhân vật rơi vào vị trí
local animationTrack1 = animator:LoadAnimation(animation1)
local animationTrack2 = animator:LoadAnimation(animation2)
animationTrack1.Priority = Enum.AnimationPriority.Movement
animationTrack2.Priority = Enum.AnimationPriority.Action
animationTrack1:Play(0.1, 5, 1)
animationTrack2:Play(10, 3, 1)
local done = false
while not done and task.wait(0.1) do
if math.abs(animationTrack2.WeightCurrent - animationTrack2.WeightTarget) < 0.001 then
print("đã đến nơi")
done = true
end
end

Phương Pháp
AdjustSpeed
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack:AdjustSpeed(speed:number):()
Tham Số
speed:number
Giá Trị Mặc Định: 1
Lợi Nhuận
()
Mẫu mã
Chơi hoạt hình trong một khoảng thời gian cụ thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)
Tốc độ hoạt hình
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
ContentProvider:PreloadAsync({ animation })
local animationTrack = animator:LoadAnimation(animation)
local normalSpeedTime = animationTrack.Length / animationTrack.Speed
animationTrack:AdjustSpeed(3)
local fastSpeedTime = animationTrack.Length / animationTrack.Speed
print("Với tốc độ bình thường, hoạt hình sẽ phát trong", normalSpeedTime, "giây")
print("Với tốc độ gấp 3 lần, hoạt hình sẽ phát trong", fastSpeedTime, "giây")

AdjustWeight
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack:AdjustWeight(
weight:number, fadeTime:number
):()
Tham Số
weight:number
Giá Trị Mặc Định: 1
fadeTime:number
Giá Trị Mặc Định: 0.100000001
Lợi Nhuận
()
Mẫu mã
Thay đổi trọng số AnimationTrack
local function changeWeight(animationTrack, weight, fadeTime)
animationTrack:AdjustWeight(weight, fadeTime)
local startTime = tick()
while math.abs(animationTrack.WeightCurrent - weight) > 0.001 do
task.wait()
end
print("Thời gian để thay đổi trọng số " .. tostring(tick() - startTime))
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
changeWeight(animationTrack, 0.6, 1)

GetMarkerReachedSignal
Các khả năng: Animation
AnimationTrack:GetMarkerReachedSignal(name:string):RBXScriptSignal
Tham Số
name:string
Lợi Nhuận
Mẫu mã
Lắng nghe các dấu mốc Keyframe
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Tạo thể hiện "Animation" mới
local kickAnimation = Instance.new("Animation")
-- Đặt "AnimationId" của nó thành ID tài sản hình ảnh chuyển động tương ứng
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- Tải hình ảnh chuyển động vào humanoid
local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
-- Phát bản nhạc hình ảnh chuyển động
kickAnimationTrack:Play()
-- Nếu một sự kiện đã được định nghĩa cho hình ảnh chuyển động, kết nối nó đến "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)

GetParameter
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack:GetParameter(key:string):Variant
Tham Số
key:string
Lợi Nhuận
Variant

GetParameterDefaults
Các khả năng: Animation
AnimationTrack:GetParameterDefaults():Dictionary
Lợi Nhuận

GetTargetInstance
Các khả năng: Animation
AnimationTrack:GetTargetInstance(name:string):Instance?
Tham Số
name:string
Lợi Nhuận

GetTargetNames
Các khả năng: Animation
AnimationTrack:GetTargetNames():{any}
Lợi Nhuận

GetTimeOfKeyframe
Các khả năng: Animation
AnimationTrack:GetTimeOfKeyframe(keyframeName:string):number
Tham Số
keyframeName:string
Lợi Nhuận
Mẫu mã
Nhảy Đến Khung Hình Chính
local function jumpToKeyframe(animationTrack, keyframeName)
local timePosition = animationTrack:GetTimeOfKeyframe(keyframeName)
if not animationTrack.IsPlaying then
animationTrack:Play()
end
animationTrack.TimePosition = timePosition
end
local ANIMATION_ID = 0
local KEYFRAME_NAME = "Kiểm Tra"
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. ANIMATION_ID
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
jumpToKeyframe(animationTrack, KEYFRAME_NAME)

Play
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack:Play(
fadeTime:number, weight:number, speed:number
):()
Tham Số
fadeTime:number
Giá Trị Mặc Định: 0.100000001
weight:number
Giá Trị Mặc Định: 1
speed:number
Giá Trị Mặc Định: 1
Lợi Nhuận
()
Mẫu mã
Chơi hoạt hình trong một khoảng thời gian cụ thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)
Đóng băng hoạt ảnh tại vị trí
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
freezeAnimationAtTime(animationTrack, 0.5)
freezeAnimationAtPercent(animationTrack, 50)

SetParameter
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack:SetParameter(
key:string, value:Variant
):()
Tham Số
key:string
value:Variant
Lợi Nhuận
()

SetTargetInstance
Các khả năng: Animation
AnimationTrack:SetTargetInstance(
name:string, target:Instance?
):()
Tham Số
name:string
target:Instance?
Lợi Nhuận
()

Stop
Các khả năng: Animation
Quyền sử dụng chế độ mô phỏng
AnimationTrack:Stop(fadeTime:number):()
Tham Số
fadeTime:number
Giá Trị Mặc Định: 0.100000001
Lợi Nhuận
()
Mẫu mã
Dừng AnimationTrack
local function fadeOut(animationTrack, fadeTime)
animationTrack:Stop(fadeTime)
local startTime = tick()
while animationTrack.WeightCurrent > 0 do
task.wait()
end
local timeTaken = tick() - startTime
print("Thời gian để trọng số được đặt lại: " .. tostring(timeTaken))
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
fadeOut(animationTrack, 1)

Sự Kiện
DidLoop
Các khả năng: Animation
AnimationTrack.DidLoop():RBXScriptSignal
Mẫu mã
Chạy AnimationTrack trong một số vòng lặp
local function playForNumberLoops(animationTrack, number)
animationTrack.Looped = true
animationTrack:Play()
local numberOfLoops = 0
local connection = nil
connection = animationTrack.DidLoop:Connect(function()
numberOfLoops = numberOfLoops + 1
print("vòng lặp: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- quan trọng để ngắt kết nối khi không còn cần thiết
end
end)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playForNumberLoops(animationTrack, 5)

Ended
Các khả năng: Animation
AnimationTrack.Ended():RBXScriptSignal
Mẫu mã
Hoạt ảnh đã kết thúc
local InsertService = game:GetService("InsertService")
local Players = game:GetService("Players")
-- Tạo một mô hình NPC để hoạt hình.
local npcModel = Players:CreateHumanoidModelFromUserIdAsync(129687796)
npcModel.Name = "JoeNPC"
npcModel.Parent = workspace
npcModel:MoveTo(Vector3.new(0, 15, 4))
local humanoid = npcModel:WaitForChild("Humanoid")
-- Tải một hoạt hình.
local animationModel = InsertService:LoadAsset(2510238627)
local animation = animationModel:FindFirstChildWhichIsA("Animation", true)
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
-- Kết nối với sự kiện Dừng lại. Điều này xảy ra khi hoạt hình dừng lại
-- theo cách riêng của nó, hoặc chúng ta gọi Stop một cách rõ ràng.
animationTrack.Stopped:Connect(function()
print("Hoạt hình dừng lại")
end)
-- Kết nối với sự kiện Kết thúc. Điều này xảy ra khi hoạt hình hoàn toàn
-- kết thúc ảnh hưởng đến thế giới. Trong trường hợp này, nó sẽ xảy ra 3 giây
-- sau khi chúng ta gọi animationTrack:Stop vì chúng ta truyền vào một
-- độ mờ 3 giây.
animationTrack.Ended:Connect(function()
print("Hoạt hình kết thúc")
animationTrack:Destroy()
end)
-- Chạy, cho nó một chút thời gian để phát, rồi dừng lại.
print("Gọi Play")
animationTrack:Play()
task.wait(10)
print("Gọi Stop")
animationTrack:Stop(3)

KeyframeReached
Đã Lỗi Thời

Stopped
Các khả năng: Animation
AnimationTrack.Stopped():RBXScriptSignal
Mẫu mã
AnimationTrack Dừng lại
local function yieldPlayAnimation(animationTrack, fadeTime, weight, speed)
animationTrack:Play(fadeTime, weight, speed)
animationTrack.Stopped:Wait()
print("Hoạt hình đã dừng lại")
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
yieldPlayAnimation(animationTrack, 1, 0.6, 1)

©2026 Roblox Corporation. Roblox, logo Roblox và Powering Imagination là các nhãn hiệu đã đăng ký và chưa đăng ký của chúng tôi tại Hoa Kỳ và các quốc gia khác.