這個項目是使用 for 循環的一個實用範例。對於這座橋,玩家將觸碰一個按鈕,使橋在有限的時間內可行走,然後消失。為了創建計時器並顯示剩餘時間給玩家,你將使用 for 循環。
設置項目
這座橋可以包含在任何具有基於移動的挑戰的 Roblox 項目中,例如一個障礙賽。
創建部件
找一個地方來建造橋,例如河流或障礙賽中的大間隙。創建三個 固定 的部件,如下所示。
- TimerDisplay
- Bridge
- ButtonBridge

當不活動時,橋將是半透明的。為此,選擇 Bridge 並更改其屬性。
- Transparency = 0.8
- CanCollide = False
創建計時器顯示
當玩家穿越橋時,他們需要看到橋消失前還剩多少秒。顯示圖像或文本的一種方法是將一個名為 Surface GUI 的對象添加到部件中。Surface GUIs 也可以用來創建遊戲內標誌、自定義生命值條和庫存系統。本教程將快速介紹這一點,但可以在 教程 部分找到更多信息。
選擇 TimerDisplay 並添加一個 SurfaceGui。然後,將一個 TextLabel 作為 Surface Gui 的子項添加。

選擇 Surface GUI。在屬性中,進行以下更改:
- 更改 Face,以便你可以在玩家查看的計時器前面看到文本標籤。
選擇 TextLabel。在屬性中,進行以下更改:
- 將 Size 設置為 {1, 0},{1, 0}。
- 將 TextScaled 設置為 true。
- 將 Text 設置為空白。文本將通過腳本更新。
設置腳本
現在計時器已就位,創建一個腳本來控制橋並向玩家顯示倒計時數字。
在 Bridge 部件中,添加一個名為 TimedBridge 的新腳本。在該腳本中,創建以下變量。
local bridge = script.Parentlocal button = workspace.ButtonBridgelocal timerText = workspace.TimerDisplay.SurfaceGui.TextLabel-- 桥保持坚固的时间local timerDuration = 5
編寫觸碰交互
要使用橋,你需要創建兩個函數。一個函數將使橋可行走並顯示計時器。另一個函數將監聽玩家是否觸碰了啟動橋的按鈕。
創建一個名為 startTimer() 的新函數,並在其中添加一個打印語句。你將使用打印語句來測試你的代碼。
local timerDuration = 5local function startTimer()print("倒計時開始")end編寫一個名為 buttonPressed() 的函數,以檢查是否有一個人形觸碰了按鈕。該函數應接受一個參數 partTouched,然後在檢測到人形的部分中有一個空的 if 語句。
local function startTimer()print("倒計時開始")endlocal function buttonPressed(partTouched)local character = partTouched.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenendend為了啟動計時器,在 if 語句中調用 startTimer() 函數。
local function buttonPressed(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenstartTimer()endend在 buttonPressed() 的結尾,將 buttonPressed() 函數連接到按鈕的 Touched 事件。
local function buttonPressed(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenstartTimer()endendbutton.Touched:Connect(buttonPressed)運行項目。觸碰該部件並在輸出窗口中檢查打印語句。
故障排除提示
此時,如果橋未按預期工作,請嘗試以下其中一項。
問題:錯誤消息顯示:"... 不是 workspace 的有效成員"。
- 在 TimedBridge 腳本中,檢查所有部件的拼寫是否與 Explorer 中看到的完全相同。
問題:看不到部件。
- 確保所有三個部件都是固定的。
- 檢查橋的透明度屬性。
創建計時器
每當玩家踩上橋時,startTimer() 將使橋可行走並啟動計時器。一旦計時器達到 0,橋將變得不可行走,導致任何不夠快的人掉下去。
使橋可行走
首先,腳本需要使橋變得堅固或可碰撞,然後啟動計時器,直到它變得不可行走。
為了使橋可行走,在 startTimer() 中,將橋的透明度屬性更改為 0(不透明),並將 CanCollide 屬性設置為 true。
local function startTimer()-- 使橋可見且可行走bridge.Transparency = 0bridge.CanCollide = trueend為了創建一個倒計時的計時器,創建一個 for 循環,使用以下值。
- 控制變量:命名為 count,設置為 timerDuration。
- 結束:0
- 增量:-1
local function startTimer()-- 使橋可見且可行走bridge.Transparency = 0bridge.CanCollide = truefor count = timerDuration, 0, -1 doendend為了向玩家顯示計時器,將 timerText 中的文本更改為顯示計數,輸入 timerText.Text = count。每次 for 循環進行一次迭代時,它將向玩家顯示計時器中的下一個數字。
for count = timerDuration, 0, -1 dotimerText.Text = countend使用 task.wait 函數使 for 循環每秒僅運行一次。
for count = timerDuration, 0, -1 dotimerText.Text = counttask.wait(1)end運行遊戲。當你觸碰按鈕時,橋應該出現,計時器將開始,然後完成。
防止橋重新啟動
不過,請注意,如果你在按鈕上移動,計時器將不斷重新啟動。
這是因為每次你觸碰按鈕時,for 循環都會被調用並從頭開始。為了防止計時器不斷重新啟動,你需要添加一個布林值,一種變量類型,將控制是否允許再次調用 startTimer()。
布林值 的寫法與其他變量相同,但它們只能設置為 true 或 false。在這種情況下,腳本將使用布林值來檢查計時器是否當前正在運行,然後再啟動它。
在腳本的頂部,在變量下方,創建一個名為 timerActive 的變量,並將其設置為 false,因為還沒有人按下按鈕。
local timerDuration = 5local timerActive = falselocal function startTimer()為了確保計時器僅在 timerActive 布林值為 false 時啟動,向 buttonPressed() 中的 if 語句添加第二個條件。
local function buttonPressed(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid and timerActive == false thenstartTimer()endend為了防止 startTimer() 在計時器結束之前再次運行,將布林值 timerActive 設置為 true。
local function startTimer()timerActive = truebridge.Transparency = 0bridge.CanCollide = true在 startTimer() 函數中,在 for 循環之後,通過將橋的透明度更改為 0.8 和 CanCollide 設置為 false 來將橋恢復到其原始屬性。
local function startTimer()timerActive = truebridge.Transparency = 0bridge.CanCollide = true-- 倒計時的 for 循環for count = timerDuration, 0, -1 dotimerText.Text = counttask.wait(1)end-- 使橋不可行走bridge.Transparency = 0.8bridge.CanCollide = falseend最後一件事是將 timerText 更改為空字符串,如最初一樣。然後,將 timerActive 布林值設置為 false。
bridge.Transparency = 0.8bridge.CanCollide = falsetimerText.Text = ""timerActive = false進行遊戲測試,檢查橋是否可以多次使用。
完整的定時橋腳本
local bridge = script.Parent
-- 獲取按鈕,因為它在 Explorer 中被輸入
local button = workspace.ButtonBridge
-- 獲取顯示的部件
local timerPart = workspace.TimerDisplay
-- 獲取將顯示計時器的文本
local timerText = timerPart.SurfaceGui.TextLabel
-- 玩家必須穿越橋的時間
local timerDuration = 5
local timerActive = false
local function startTimer()
print("倒計時開始")
timerActive = true
bridge.Transparency = 0
bridge.CanCollide = true
-- 倒計時的 for 循環
for count = timerDuration, 0, -1 do
timerText.Text = count
task.wait(1)
end
-- 使橋不可行走
bridge.Transparency = 0.8
bridge.CanCollide = false
timerText.Text = ""
timerActive = false
end
local function buttonPressed(partTouched)
local character = partTouched.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
print("部件被觸碰")
if humanoid and timerActive == false then
print("開始計時器")
startTimer()
end
end
button.Touched:Connect(buttonPressed)總結
循環可以與不同的交互方式結合,創造有趣的遊戲時刻。在本教程中,使用一個作為倒計時的 for 循環創建了一座定時橋。在倒計時期間,玩家有有限的時間過橋。