Lập trình Roblox · Khóa 2: Lập trình Luau cơ bản – Obby
TweenInfo.new(...): thời gian, kiểu chuyển động, lặp lại| Scratch | Luau |
|---|---|
| đi tới x, y | khoi.Position = viTriMoi (dịch chuyển tức thì) |
| lướt trong 2 giây tới x, y | Tween – di chuyển mượt trong 2 giây |
local TweenService = game:GetService("TweenService") local khoi = script.Parent -- 1. Thông tin: chạy trong 3 giây local thongTin = TweenInfo.new(3) -- 2. Đích đến: lên cao thêm 20 stud local dich = {Position = khoi.Position + Vector3.new(0, 20, 0)} -- 3. Tạo và chạy local tween = TweenService:Create(khoi, thongTin, dich) tween:Play()
{Position = ...} – danh sách thuộc tính muốn thay đổi (có thể nhiều: Color, Size, Transparency…)TweenInfo.new(thoiGian, kieu, huong, soLanLap, quayNguoc, cho)
| Tham số | Ý nghĩa | Ví dụ |
|---|---|---|
| thoiGian | số giây | 3 |
| kieu | kiểu chuyển động | Enum.EasingStyle.Sine (mượt), Linear (đều), Bounce (nảy) |
| huong | tăng/giảm tốc | Enum.EasingDirection.InOut |
| soLanLap | số lần lặp, -1 = mãi mãi | -1 |
| quayNguoc | đi xong có quay về không | true |
| cho | chờ trước mỗi lần chạy (giây) | 1 |
local TweenService = game:GetService("TweenService") local buc = script.Parent local thongTin = TweenInfo.new( 3, -- 3 giây Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, -- lặp mãi mãi true, -- đi lên rồi đi xuống 1 -- nghỉ 1 giây ở mỗi đầu ) local dich = {Position = buc.Position + Vector3.new(0, 25, 0)} TweenService:Create(buc, thongTin, dich):Play()
local TweenService = game:GetService("TweenService") local khoi = script.Parent local thongTin = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true) local dich = { Size = Vector3.new(8, 8, 8), Color = Color3.fromRGB(255, 0, 200), Transparency = 0.5, } TweenService:Create(khoi, thongTin, dich):Play()
local thanh = script.Parent while true do thanh.CFrame = thanh.CFrame * CFrame.Angles(0, math.rad(3), 0) task.wait() end
CFrame.Angles(0, math.rad(3), 0) – xoay thêm 3 độ quanh trục Ytask.wait() không có số = chờ khung hình kế tiếp (rất ngắn)local TweenService = game:GetService("TweenService") local nut = script.Parent local cua = workspace.CuaTruot local daMo = false nut.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and not daMo then daMo = true nut.Color = Color3.fromRGB(0, 255, 0) local dich = {Position = cua.Position + Vector3.new(0, 12, 0)} TweenService:Create(cua, TweenInfo.new(1.5), dich):Play() end end)
Sửa bài tên lửa buổi 11: sau khi đếm ngược, tên lửa bay lên từ từ 100 stud trong 5 giây.
Làm thang máy đưa người chơi lên một bục cao 40 stud; trên đỉnh có một thanh quay chặn đường.
Khi chạm nút, 10 bậc thang lần lượt hiện ra (Transparency 1 → 0, CanCollide bật), mỗi bậc cách nhau 0,3 giây.
| Lỗi | Nguyên nhân |
|---|---|
Unable to cast value to Object | gõ sai thứ tự tham số Create(vat, thongTin, dich) |
| Khối không chạy | quên :Play() |
| Khối rơi khi Tween | chưa Anchored |
| Thanh quay giật | dùng task.wait(1) – hãy dùng task.wait() |
| Nhân vật trượt khỏi bục ngang | bục ngang di chuyển chậm lại, hoặc dùng thang máy lên xuống |
TweenService:Create(vat, TweenInfo.new(...), {ThuocTinh = giaTri}):Play()CFrame * CFrame.Angles(0, math.rad(độ), 0)