立即開始
中級教學

計分

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

在之前的教程中,您製作了多種經驗特徵,包括 漸變平台致命岩漿。這個教程將這些元素結合成一個可遊玩的體驗,讓用戶看看誰能活得最久。每當他們存活一秒鐘,他們的分數將增加一分。

設置

首先,您需要為您的體驗設置場景。複製您在之前的教程中製作的 漸變平台,讓用戶競爭,看看誰能在平台上呆得更久。

您還可以使用 致命岩漿 來在用戶從平台上掉下時殺死他們,或者讓他們直接掉下去摔死。確保您在某個地方放置一個 SpawnLocation,以便用戶可以跳到平台上開始遊玩。

在漸變平台網格上方、岩漿地板上方的生成點

玩家分數

Roblox 擁有內建的 Leaderboard 來顯示用戶統計數據。當您通過排行榜設置玩家分數時,它們將顯示在體驗屏幕的右側。

顯示用戶名和分數的排行榜

您將在後面的教程中學到更多自定義顯示信息的方法,但排行榜是 Roblox 中創建可見計分系統的最簡單方法。

最好將設置體驗狀態的腳本放在 ServerScriptService 中,因為它們會在體驗開始時自動運行。在 ServerScriptService 中,創建一個名為 SetupPoints 的腳本。

Explorer 窗口中 ServerScriptService 中的 SetupPoints 腳本

聽取玩家加入

在 Roblox 中,服務 是執行多種實用功能的對象。Players 服務具有一個名為 PlayerAdded 的事件,您可以使用它為每個加入體驗的用戶設置分數。

您可以在 game 對象中使用 GetService 函數來訪問服務。game 是一個可以在任何地方訪問的變數,其中包含您體驗中的所有內容。

  1. 使用 game:GetService("Players") 創建一個 Players 服務的變數。

  2. 創建一個名為 onPlayerAdded 的函數,並為傳入的玩家設置一個參數。

  3. onPlayerAdded 函數連接到 PlayerAdded 事件。


    local Players = game:GetService("Players")
    local function onPlayerAdded(player)
    end
    Players.PlayerAdded:Connect(onPlayerAdded)

創建統計資料文件夾

為了讓用戶的分數顯示在排行榜中,您只需在他們的 Player 對象中創建一個名為 "leaderstats" 的新 Folder,並將他們的分數放在那裡。可以通過 Instance.new() 函數在腳本中創建新對象。

  1. 使用 Instance.new("Folder") 創建一個新的 Folder 對象,將結果存儲在一個名為 leaderstats 的新變數中。


    local function onPlayerAdded(player)
    local leaderstats = Instance.new("Folder")
    end
  2. leaderstats 的 Name 屬性設置為 "leaderstats"

  3. leaderstats 作為父級分配給 player


    local Players = game:GetService("Players")
    local function onPlayerAdded(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    end
    Players.PlayerAdded:Connect(onPlayerAdded)

創建分數

排行榜系統會讀取 leaderstats 文件夾中的任何值並顯示找到的內容。

要添加一個跟踪玩家分數的統計數據,可以將新的 IntValue 對象作為父項添加到 leaderstats 文件夾中。值對象的名稱將與其當前值並排顯示。

  1. 使用名為 points 的變數創建一個新的 IntValue 對象,使用 Instance.new()
  2. Name 設置為 "Points"
  3. Value 設置為 0;這是排行榜初始顯示的玩家分數。
  4. points 對象作為父項添加到 leaderstats 文件夾中。

local Players = game:GetService("Players")
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
end
Players.PlayerAdded:Connect(onPlayerAdded)

測試您的體驗,您應該會看到排行榜在右上角出現,顯示您的用戶名稱和分數。

計算時間

每個用戶都應該因為每存活一秒而獲得一分。可以使用 while 循環和 task.wait() 函數每秒更新一次分數的值。

  1. 在腳本的結尾處,創建一個 while 循環,以 true 作為條件。
  2. 在循環中,使用 task.wait() 等待 1 秒。

Players.PlayerAdded:Connect(onPlayerAdded)
while true do
task.wait(1)
end

玩家列表

要為體驗中的每個用戶運行代碼,您需要遍歷 GetPlayers 函數返回的用戶 數組

數組是按照順序存儲的項目列表。每個項目可以通過其 索引 位置進行訪問,索引從 1 開始。您可以通過在前面加上 # 獲取數組的長度。

  1. Players:GetPlayers() 的結果存儲在一個 playerList 變數中。
  2. 創建一個 for 循環,起始值為 1,結束值為 #playerList,這樣您就可以為每個玩家獲得一次循環的迭代。

while true do
task.wait(1)
local playerList = Players:GetPlayers()
for currentPlayer = 1, #playerList do
-- 在這裡添加您的邏輯,針對 playerList 中的每個玩家
end
end

獎勵分數

要在 for 循環中獲得每個用戶的一分,您需要從數組中取出用戶,並將 1 加到他們的 Points 對象中。

存儲在數組中的對象可以使用 方括號 訪問,例如,playerList 數組中的第一個項目可以通過 playerList[1] 訪問。如果您在 for 循環中寫 playerList[currentPlayer],則可以在循環的每次迭代中遍歷數據列出的每個用戶。

  1. 將位於 playerList[currentPlayer] 的用戶存儲在一個名為 player 的變數中。
  2. 將用戶的 Points 對象存儲在一個名為 points 的變數中。
  3. Value 屬性的值設置為 points.Value + 1

while true do
task.wait(1)
local playerList = Players:GetPlayers()
for currentPlayer = 1, #playerList do
local player = playerList[currentPlayer]
local points = player.leaderstats.Points
points.Value += 1
end
end

測試您的體驗,您應該會發現排行榜以每秒 1 的速度顯示您的玩家分數。

監聽角色

這個體驗的目標是看看誰能存活最久,因此死去的用戶需要將其分數重置為 0。

您需要獲取用戶的 Character 模型才能檢測他們何時死去。此模型僅在 Player 對象加載後添加到體驗中,您可以使用 CharacterAdded 事件來監聽何時可以使用角色。創建一個名為 onCharacterAdded 的函數,帶有兩個參數:一個用於角色,一個用於玩家。


local Players = game:GetService("Players")
local function onCharacterAdded(character, player)
end
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")

雖然您在 onCharacterAdded 函數的參數中包含了用戶,但實際的 CharacterAdded 事件僅返回角色,而不返回關聯的用戶。要同時傳遞 player 對象,請對事件連接使用一個 匿名函數


local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
onCharacterAdded(character, player)
end)
end

重置分數

當用戶死亡時,他們的 Humanoid 會自動觸發 Died 事件。您可以使用這個事件來確定何時重置他們的分數。

Humanoid 位於角色模型中,但該模型的內容僅在用戶生成時組裝。為了安全地等候 Humanoid 對象加載,請使用 WaitForChild() 函數。您可以在任何父級對象上調用它,傳遞您等待的子對象的字符串名稱。使用 character:WaitForChild("Humanoid") 等待 Humanoid。


local Players = game:GetService("Players")
local function onCharacterAdded(character, player)
local humanoid = character:WaitForChild("Humanoid")
end

您需要連接到 Died 事件的函數非常簡短,僅在此處使用,因此您也可以再次使用匿名函數。

  1. 將一個新的匿名函數連接到 Humanoid 的 Died 事件。
  2. 在匿名函數中,創建一個名為 points 的變數,用於玩家的 Points 對象。
  3. Value 屬性的值設置為 0

local Players = game:GetService("Players")
local function onCharacterAdded(character, player)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
local points = player.leaderstats.Points
points.Value = 0
end)
end

測試一下,您將看到用戶的分數在他們死亡時重置。

檢查玩家

如果用戶即使在死亡時也不斷獲得分數,這顯然與體驗的精神不符,因此代碼需要檢查用戶在頒發分數之前是否活著。

您需要在 onPlayerAdded 函數中定義一個 屬性,以檢查用戶是否活著。此時,用戶尚未活著並生成,因為他們的角色模型仍然需要添加。

屬性允許您使用自己的數據自定義 Roblox 中的對象。屬性由名稱和值組成。您可以使用 SetAttribute 函數在任何對象上創建一個。對 player 調用 SetAttribute 以創建一個名為 "IsAlive" 的新 屬性,其值為 false


local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
player:SetAttribute("IsAlive", false)
player.CharacterAdded:Connect(function(character)
onCharacterAdded(character, player)
end)
end

一旦用戶的角色模型重生,IsAlive 的值需要更改為 true,以便用戶可以重新開始獲得分數。

  1. onCharacterAdded 中,將 playerIsAlive 屬性設置為 true
  2. onCharacterDied 中,將 playerIsAlive 屬性設置為 false

local Players = game:GetService("Players")
local function onCharacterAdded(character, player)
player:SetAttribute("IsAlive", true)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
local points = player.leaderstats.Points
points.Value = 0
player:SetAttribute("IsAlive", false)
end)
end

最後,在腳本末尾的 while 循環之前應檢查 IsAlive,以頒發任何分數。GetAttribute 函數接受屬性的名稱並返回該屬性的值。在 while 循環中,將頒發分數的代碼包裹在一個 if 語句中,條件為 player:GetAttribute("IsAlive")


while true do
task.wait(1)
local playerList = Players:GetPlayers()
for currentPlayer = 1, #playerList do
local player = playerList[currentPlayer]
if player:GetAttribute("IsAlive") then
local points = player.leaderstats.Points
points.Value += 1
end
end
end

現在測試一下您的體驗,您應該會發現用戶在生存的每一秒中獲得分數,當不活著時保持 0。邀請您的朋友們一起玩,看看誰能獲得最高分。

這僅僅是開始:您可以繼續改善用戶的體驗。以下是一些建議:

  • 將所有平台的代碼放入一個腳本中,使其更易於更新。
  • 創建一個大廳區域,讓用戶等待被傳送到體驗區域,讓用戶可以同時開始。
  • 公布每輪的勝者。

最終代碼


local Players = game:GetService("Players")
local function onCharacterAdded(character, player)
player:SetAttribute("IsAlive", true)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
local points = player.leaderstats.Points
points.Value = 0
player:SetAttribute("IsAlive", false)
end)
end
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
player:SetAttribute("IsAlive", false)
player.CharacterAdded:Connect(function(character)
onCharacterAdded(character, player)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
while true do
task.wait(1)
local playerList = Players:GetPlayers()
for i = 1, #playerList do
local player = playerList[i]
if player:GetAttribute("IsAlive") then
local points = player.leaderstats.Points
points.Value += 1
end
end
end
©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。