如何在 roblox 上制作排行榜?
9 回答
需要在每个玩家中插入一个名为“leaderstats”的值,使用带有 PlayerAdded 事件的脚本。在 leaderstats 值中,您可以放置 IntValues - 它们的名称将显示为标题,它们的值将显示为玩家的统计信息。
要更改这些统计信息,您需要向创建 leaderstats 值的脚本添加不同的函数和/或事件。
将脚本插入工作区,然后在代码中键入:
function Onplayerentered(player)
local leaderstats = Instance.new("IntValue")
leaderstats.Parent = player
leaderstats.Value = 0
leaderstats.Name = "leaderstats"
local stat = Instance.new("IntValue")
stat.Name = "" -- Put name here in between the quotations
stat.Value = -- Put the starting Value#
end
game:GetService("Players").ChildAdded:Connect(Onplayerentered)
- 调出 roblox 插入工具栏。
- 选择排行榜。
- 您可以自定义脚本以满足您的需求!
Roblox 排行榜是一个很长的脚本,幸运的是,该脚本允许我们轻松添加和删除 leaderstats。要添加排行榜,请在玩家对象内插入 IntValue,要添加统计信息,请在前导数据中插入 IntValue。
Roblox 上的大多数游戏都希望每个玩家都拥有相同的排行榜。所以大多数人使用 PlayerAdded 事件并创建排行榜
function Onplayererntered(player)
local leaderstats = Instance.new("IntValue")
leaderstats.Pareny = player
leaderstats.Value = 0
leaderstats.Name = "leaderboard"
local stat = Instance.new("IntValue")
statname = "Cash"
stat.Value = 100
end
您首先必须在服务器脚本服务中创建一个脚本并将其命名为您想要的任何名称,然后将其写入脚本中(确保其正常脚本不是本地脚本)
game:GetService("Players").PlayerAdded:Connect(function() --make the function start when new player joins
local player = game.Players.PlayerAdded --make player variable
local leaderstats = instance.new("Folder", player) --make new folder and set it's parent to the player
local money = instance.new("IntValue", leaderstats) --create new value for the stat and set it's parent to the leaderstats folder (you can create as many as u want)
money.name = "Money" --make the name of the value
money.Value = 0 --make the value's value
end)
这段代码很简单,有很多注释可以解释它我希望它有帮助。
ROBLOX 将排行榜定义为名为“leaderstats”并位于玩家对象中的对象。排行榜统计定义为 leaderstats 对象(Player>leaderstats>ValueObject)中的一个值对象。因此,让我们编写一个函数,为玩家创建一个带有“现金”统计数据的排行榜。
local function createLeaderboard(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local cash = Instance.new("IntValue", stats)
cash.Name = "Cash"
stats.Parent = player
end
然后我们需要完成这项工作。我们需要将此函数连接到来自“Players”对象的“PlayerAdded”事件。
local players = game:WaitForChild("Players")
players.PlayerAdded:connect(createLeaderboard)
基本上就是这样。请注意,上面直接显示的代码中的第 3 行等效于:
players.PlayerAdded:connect(function(player)
createLeaderboard(player)
end)
整个脚本如下所示:
local players = game:WaitForChild("Players")
local function createLeaderboard(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local cash = Instance.new("IntValue", stats)
cash.Name = "Cash"
stats.Parent = player
end
players.PlayerAdded:connect(createLeaderboard)
建议将脚本放在“ServerScriptService”中。
将脚本插入 ServerScriptService 并粘贴以下代码:
plrEntered = function(plr)
local ls = Instance.new('IntValue') --Leaderstats
ls.Parent = plr
ls.Value = 0
ls.Name = 'leaderstats'
local stat = Instance.new('IntValue')
stat.Name = 'Money' -- Change to the value you want
stat.Value = 0 -- Add the starting value
end
game:GetService'Players'.PlayerAdded(plrEntered)
function stats(plr)
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local coins = Instance.new("IntValue")
Coins.Name = "coins"
Coins.Parent = leaderstats
end)
game.Players.PlayyerAdded:Connect(stats)