ในบทแนะนำก่อนหน้า คุณได้สร้างฟีเจอร์ประสบการณ์ที่หลากหลายรวมถึง แพลตฟอร์มที่จางหาย และ ลาวาที่อันตราย บทแนะนำนี้เชื่อมต่อสิ่งเหล่านี้เข้าด้วยกันเป็นประสบการณ์ที่เล่นได้ซึ่งผู้ใช้สามารถเห็นว่าผู้ใดสามารถอยู่รอดได้นานที่สุด ทุก ๆ วินาทีที่พวกเขายังมีชีวิตอยู่ คะแนนจะถูกเพิ่มไปยังคะแนนของพวกเขา
ตั้งค่า
ก่อนอื่น คุณจะต้องตั้งค่าฉากสำหรับประสบการณ์ของคุณ ทำการทำสำเนา แพลตฟอร์มที่จางหาย ที่คุณสร้างขึ้นในบทแนะนำก่อนหน้าและให้ผู้ใช้แข่งขันกันเพื่ออยู่บนกระดานแพลตฟอร์มนานที่สุดเท่าที่จะทำได้
คุณยังสามารถใช้ ลาวาที่อันตราย เพื่อฆ่าผู้ใช้เมื่อพวกเขาตกจากแพลตฟอร์ม หรือเพียงแค่ปล่อยให้พวกเขาตกลงไปในความหายนะ ตรวจสอบให้แน่ใจว่าคุณวาง SpawnLocation ในที่ที่ผู้ใช้สามารถกระโดดขึ้นไปบนแพลตฟอร์มเพื่อเริ่มเล่น

คะแนนของผู้เล่น
Roblox มี Leaderboard ในตัวสำหรับแสดงสถิติของผู้ใช้ เมื่อคุณตั้งค่าคะแนนผู้เล่นผ่านบอร์ดผู้นำ พวกเขาจะแสดงทางด้านขวาของหน้าจอในประสบการณ์

คุณจะได้เรียนรู้วิธีการที่ปรับแต่งได้มากขึ้นในการแสดงข้อมูลในบทแนะนำต่อ ๆ ไป แต่บอร์ดผู้นำเป็นวิธีที่ง่ายที่สุดในการทำให้ระบบการให้คะแนนที่มองเห็นได้ใน Roblox
ดีที่สุดคือให้ใส่สคริปต์ที่ตั้งค่าสถานะประสบการณ์ลงใน ServerScriptService เพราะพวกมันจะทำงานโดยอัตโนมัติเมื่อเริ่มต้นประสบการณ์ ใน ServerScriptService ให้สร้างสคริปต์ชื่อว่า SetupPoints

ฟังผู้เล่น
ใน Roblox service เป็นวัตถุที่ทำฟังก์ชันที่มีประโยชน์หลากหลาย Players service มีเหตุการณ์ที่เรียกว่า PlayerAdded ซึ่งคุณสามารถใช้เพื่อสร้างคะแนนสำหรับผู้ใช้แต่ละคนที่เข้าร่วมประสบการณ์
คุณสามารถเข้าถึงบริการได้ด้วยฟังก์ชัน GetService ในวัตถุ game game เป็นตัวแปรที่เข้าถึงได้จากทุกที่ซึ่งมีทุกอย่างในประสบการณ์ของคุณ
สร้างตัวแปรสำหรับบริการ Players โดยใช้ game:GetService("Players")
สร้างฟังก์ชันชื่อ onPlayerAdded โดยมีพารามิเตอร์สำหรับผู้เล่นที่เข้ามา
เชื่อมต่อฟังก์ชัน onPlayerAdded กับเหตุการณ์ PlayerAdded
local Players = game:GetService("Players")local function onPlayerAdded(player)endPlayers.PlayerAdded:Connect(onPlayerAdded)
สร้างโฟลเดอร์สถิติ
ในการทำให้คะแนนของผู้ใช้แสดงในบอร์ดผู้นำ คุณเพียงแค่ต้องสร้าง Folder ใหม่ในวัตถุ Player ของพวกเขาเรียกว่า "leaderstats" และใส่คะแนนของพวกเขาไว้ที่นั่น วัตถุใหม่สามารถสร้างจากภายในสคริปต์ผ่านฟังก์ชัน Instance.new()
สร้างวัตถุ Folder ใหม่โดยใช้ Instance.new("Folder") และเก็บผลลัพธ์ในตัวแปรใหม่ชื่อ leaderstats
local function onPlayerAdded(player)local leaderstats = Instance.new("Folder")endตั้งค่า property ชื่อของ leaderstats เป็น "leaderstats"
Parent leaderstats ไปที่ player
local Players = game:GetService("Players")local function onPlayerAdded(player)local leaderstats = Instance.new("Folder")leaderstats.Name = "leaderstats"leaderstats.Parent = playerendPlayers.PlayerAdded:Connect(onPlayerAdded)
สร้างคะแนน
ระบบบอร์ดผู้นำจะแสดงค่าใด ๆ ในโฟลเดอร์ leaderstats และแสดงสิ่งที่พบ
เพื่อเพิ่มสถิติที่จะติดตามคะแนนของผู้เล่น วัตถุ IntValue ใหม่สามารถถูก parent ไปยังโฟลเดอร์ leaderstats ชื่อของวัตถุค่า จะแสดงข้างค่าปัจจุบันของมัน
- ตั้งค่า Name เป็น "Points"
- ตั้งค่า Value เป็น 0; นี่คือสิ่งที่บอร์ดผู้นำจะแสดงในตอนแรกสำหรับผู้เล่น
- Parent วัตถุ 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 loop และฟังก์ชัน task.wait() สามารถใช้เพื่ออัพเดทค่าของคะแนนทุก ๆ วินาที
- ที่จุดสิ้นสุดของสคริปต์ สร้าง while loop โดยมี true เป็นเงื่อนไข
- ในลูปนั้น task.wait() เป็นเวลา 1 วินาที
Players.PlayerAdded:Connect(onPlayerAdded)while true dotask.wait(1)end
รายชื่อผู้เล่น
เพื่อให้สามารถเรียกใช้โค้ดสำหรับผู้ใช้ทุกคนในประสบการณ์ คุณจำเป็นต้องวนซ้ำผ่าน array ของผู้ใช้ที่คืนค่าจากฟังก์ชัน GetPlayers
อาร์เรย์คือรายการของรายการที่จัดเก็บในลำดับ รายการแต่ละรายการสามารถเข้าถึงได้ตามตำแหน่ง index โดยเริ่มจาก 1 คุณสามารถหารายการของอาร์เรย์ได้โดยการเติมด้วย #
- เก็บผลลัพธ์ของ Players:GetPlayers() ในตัวแปร playerList
- สร้าง for loop โดยมีค่าตั้งต้นที่ 1 และค่าจบที่ #playerList เพื่อให้คุณได้รับรอบของลูปหนึ่งครั้งต่อผู้เล่นแต่ละคน
while true dotask.wait(1)local playerList = Players:GetPlayers()for currentPlayer = 1, #playerList do-- เพิ่มตรรกะของคุณที่นี่สำหรับแต่ละผู้เล่นใน playerListendend
มอบคะแนน
เพื่อมอบคะแนนให้กับผู้ใช้แต่ละคนในลูป for คุณจะต้องเอาผู้ใช้ออกจากอาร์เรย์และเพิ่ม 1 ให้กับวัตถุ Points ที่เก็บอยู่ในโฟลเดอร์ leaderstats ของพวกเขา
วัตถุที่เก็บอยู่ในอาร์เรย์เข้าถึงได้โดยใช้ วงเล็บเหลี่ยม - ตัวอย่างเช่น รายการแรกในอาร์เรย์ playerList สามารถเข้าถึงได้ด้วย playerList[1] หากคุณเขียน playerList[currentPlayer] ในลูป for คุณสามารถเดินผ่านผู้ใช้แต่ละคนในรายการได้ในทุก ๆ รอบของลูป
- เก็บผู้ใช้อยู่ที่ playerList[currentPlayer] ในตัวแปรที่เรียกว่า player
- เก็บวัตถุ Points ของผู้ใช้ในตัวแปรที่เรียกว่า points
while true dotask.wait(1)local playerList = Players:GetPlayers()for currentPlayer = 1, #playerList dolocal player = playerList[currentPlayer]local points = player.leaderstats.Pointspoints.Value += 1endend
ทดสอบประสบการณ์ของคุณและคุณควรพบว่าบอร์ดผู้นำแสดงคะแนนของผู้เล่นของคุณเพิ่มขึ้น 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 จะอยู่ภายในโมเดล Character แต่เนื้อหาของโมเดลนั้นจะถูกประกอบขึ้นเฉพาะเมื่อผู้ใช้เกิดขึ้น เพื่อให้โค้ดของคุณรออย่างปลอดภัยสำหรับวัตถุ Humanoid ให้ใช้ฟังก์ชัน WaitForChild() คุณสามารถเรียกใช้มันในวัตถุพาเรนต์ใด ๆ โดยส่งชื่อของลูกค้าที่คุณรออยู่เป็นสตริง สร้างตัวแปรเพื่อรอ Humanoid โดยใช้ character:WaitForChild("Humanoid")
local Players = game:GetService("Players")
local function onCharacterAdded(character, player)
local humanoid = character:WaitForChild("Humanoid")
end
ฟังก์ชันที่คุณต้องเชื่อมต่อกับเหตุการณ์ Died ค่อนข้างสั้นและจะจำเป็นเฉพาะที่นี่ ดังนั้นคุณสามารถใช้ฟังก์ชันที่ไม่มีชื่ออีกครั้ง
- เชื่อมต่อฟังก์ชันที่ไม่มีชื่อใหม่กับเหตุการณ์ Died ของ Humanoid
- ในฟังก์ชันที่ไม่มีชื่อ สร้างตัวแปรที่เรียกว่า points สำหรับวัตถุ Points ของผู้เล่น
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
ทดสอบสิ่งนี้และคุณจะเห็นคะแนนของผู้ใช้รีเซ็ตเมื่อพวกเขาตาย
ตรวจสอบผู้เล่น
หากผู้ใช้ยังคงได้รับคะแนนแม้จะตาย มันคงไม่อยู่ในจิตวิญญาณของประสบการณ์ ดังนั้นโค้ดจำเป็นต้องตรวจสอบว่าผู้ใช้ยังมีชีวิตอยู่ก่อนที่จะมอบคะแนน
คุณเริ่มต้นด้วยการกำหนด attribute ในฟังก์ชัน onPlayerAdded ซึ่งสามารถใช้เพื่อตรวจสอบว่าผู้ใช้ยังมีชีวิตอยู่หรือไม่ ในขณะนี้ ผู้ใช้ยังไม่เป็นชีวิตและเกิดขึ้น เนื่องจากโมเดลตัวละครของพวกเขายังคงต้องถูกเพิ่มเข้ามา
Attributes ช่วยให้คุณปรับแต่งวัตถุใน Roblox ด้วยข้อมูลของคุณเอง Attribute ประกอบด้วยชื่อและค่า คุณสามารถสร้างหนึ่งในวัตถุใด ๆ โดยใช้ฟังก์ชัน SetAttribute เรียก SetAttribute ใน player เพื่อสร้าง attribute ใหม่ที่เรียกว่า "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 เพื่อให้ผู้ใช้สามารถเริ่มได้รับคะแนนอีกครั้ง
- ใน onCharacterAdded ตั้งค่า attribute IsAlive ของ player เป็น true
- ใน onCharacterDied ตั้งค่า attribute IsAlive ของ player เป็น 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
สุดท้าย IsAlive ควรได้รับการ ตรวจสอบ ก่อนที่จะมีการมอบคะแนนใน while loop ที่ส่วนท้ายของสคริปต์ ฟังก์ชัน GetAttribute รับชื่อของ attribute และคืนค่าของมัน ใน while loop ห่อหุ้มโค้ดสำหรับการมอบคะแนนใน if statement ด้วยเงื่อนไข player:GetAttribute("IsAlive")
while true dotask.wait(1)local playerList = Players:GetPlayers()for currentPlayer = 1, #playerList dolocal player = playerList[currentPlayer]if player:GetAttribute("IsAlive") thenlocal points = player.leaderstats.Pointspoints.Value += 1endendend
ทดสอบประสบการณ์ของคุณตอนนี้และคุณควรพบว่าผู้ใช้ได้รับคะแนนทุก ๆ วินาทีที่พวกเขายังมีชีวิต และยังคงเป็น 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