Leaderstats¶
Leaderstats/排行榜是出现在玩家屏幕右上角的面板。如果你习惯在 Roblox 上玩游戏,那么你肯定在许多游戏中见过这个。

如何创建?¶
创建一个 Leaderstats 非常简单。首先,我们将在 ServerScriptService 中添加一个 script。

为了创建 Leaderstats,我们必须在每个玩家内部添加一个名为 leaderstats 的文件夹。创建了文件夹后,我们将添加值对象,如 IntValue,NumberValue,StringValue 等。
在脚本中,我们将获取每个加入游戏的玩家并为其创建 Leaderstats。为了获取每个玩家,我们将连接一个函数到 PlayerAdded。在函数中,我们将为 Leaderstats 创建文件夹并在其中设置值。
local function stats_handler(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(stats_handler)
在 stats_handler 的第一部分中,我们使用 instance.new() 创建了一个名为 leaderstats 的文件夹,并将其设置为 Player 的父级。player 是由 PlayerAdded 返回的。在第二部分中,我们创建了一个 IntValue,将其命名为 "Points" 并将其父级设置为 leaderstats 文件夹。
现在,如果按下 f5 运行游戏,你会在屏幕上看到 Leaderstats。
注意
文件夹的名称必须确切地命名为 leaderstats。如果有任何拼写错误或大小写错误,Roblox 引擎将不会创建排行榜。
设置值¶
一旦创建了 Leaderstats,你可以通过更改 IntValue 的 Value 属性来设置值。
local function stats_handler(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
points.Value = 100
end
game.Players.PlayerAdded:Connect(stats_handler)
现在,如果运行游戏,你的分数将为 100。
实施¶
作为使用的示例,创建一个位于 Workspace 中的部分,并在其上添加 ClickDetector。你可以创建一个单独的脚本或使用普通的脚本。我们将使用 ClickDetector 的 MouseClick 事件,该事件在玩家与其交互时触发。此事件还返回 Player。

workspace.Part.ClickDetector.MouseClick:Connect(function(player)
player.leaderstats..Points.Value = player.leaderstats.Points.Value + 1
end)
结语!¶
和往常一样,希望您喜欢阅读并根据您的需求使用 Leaderstats。无论您学到什么,请在现场进行实践。如果有任何错误、拼写错误等,请报告文章!"