概要
属性
方法
活动
PlayerAdded(player: Player):RBXScriptSignal |
PlayerMembershipChanged(player: Player):RBXScriptSignal |
PlayerRemoving(player: Player,reason: Enum.PlayerExitReason):RBXScriptSignal |
UserSubscriptionStatusChanged(user: Player,subscriptionId: string):RBXScriptSignal |
API 参考
属性
CharacterAutoLoads
代码示例
玩家重生计时器
local Players = game:GetService("Players")
-- 将 CharacterAutoLoads 设置为 false
Players.CharacterAutoLoads = false
-- 在玩家死亡时从工作区移除玩家的角色
Players.PlayerAdded:Connect(function(player)
while true do
local char = player.CharacterAdded:Wait()
char.Humanoid.Died:Connect(function()
char:Destroy()
end)
end
end)
-- 每 10 秒重生所有死亡的玩家
while true do
local players = Players:GetChildren()
-- 检查每个玩家是否死亡,通过检查他们是否没有角色,如果死亡则加载该玩家的角色
for _, player in pairs(players) do
if not workspace:FindFirstChild(player.Name) then
player:LoadCharacterAsync()
end
end
-- 等待 10 秒直到下一个重生检查
task.wait(10)
endlocalPlayer
NumPlayers
numPlayers
方法
BanAsync
参数
返回
()
代码示例
禁止用户
local Players = game:GetService("Players")
if shouldBeBanned(player) then
local banHistoryPages = Players:GetBanHistoryAsync(player.UserId)
local duration = 86400 -- 24小时(秒)。建议:根据用户的BanHistory使用自己的逻辑
local config: BanConfigType = {
UserIds = { player.UserId },
Duration = duration,
DisplayReason = "您违反了社区指南第5条",
PrivateReason = "在这里放任何用户不应知道但对您的记录有帮助的内容",
ExcludeAltAccounts = false,
ApplyToUniverse = true,
}
local success, err = pcall(function()
return Players:BanAsync(config)
end)
print(success, err)
endChat
CreateHumanoidModelFromDescription
CreateHumanoidModelFromDescriptionAsync
Players:CreateHumanoidModelFromDescriptionAsync(
description:HumanoidDescription, rigType:Enum.HumanoidRigType, assetTypeVerification:Enum.AssetTypeVerification
参数
| 默认值:"Default" |
返回
代码示例
从描述创建人形模型
game.Players:CreateHumanoidModelFromDescriptionAsync(Instance.new("HumanoidDescription"), Enum.HumanoidRigType.R15).Parent =
game.WorkspaceCreateHumanoidModelFromUserId
CreateHumanoidModelFromUserIdAsync
GetBanHistoryAsync
GetCharacterAppearanceAsync
GetCharacterAppearanceInfoAsync
参数
代码示例
示例返回角色外观字典
local result = {
playerAvatarType = "R15",
defaultPantsApplied = false,
defaultShirtApplied = false,
scales = {
bodyType = 0,
head = 1,
height = 1.05,
proportion = 0,
depth = 0.92,
width = 0.85,
},
bodyColors = {
leftArmColorId = 1030,
torsoColorId = 1001,
rightArmColorId = 1030,
headColorId = 1030,
leftLegColorId = 1001,
rightLegColorId = 1001,
},
assets = {
{
id = 1031492,
assetType = {
name = "Hat",
id = 8,
},
name = "条纹帽",
},
{
id = 13062491,
assetType = {
name = "面部配饰",
id = 42,
},
name = "法式视力",
},
{
id = 16598440,
assetType = {
name = "颈部配饰",
id = 43,
},
name = "红色领结",
},
{
id = 28999228,
assetType = {
name = "脸",
id = 18,
},
name = "愉快的惊喜",
},
{
id = 86896488,
assetType = {
name = "衬衫",
id = 11,
},
name = "昂贵的红色晚礼服外套",
},
{
id = 86896502,
assetType = {
name = "裤子",
id = 12,
},
name = "昂贵的红色晚礼服裤子",
},
{
id = 376530220,
assetType = {
name = "左臂",
id = 29,
},
name = "ROBLOX 男孩左臂",
},
{
id = 376531012,
assetType = {
name = "右臂",
id = 28,
},
name = "ROBLOX 男孩右臂",
},
{
id = 376531300,
assetType = {
name = "左腿",
id = 30,
},
name = "ROBLOX 男孩左腿",
},
{
id = 376531703,
assetType = {
name = "右腿",
id = 31,
},
name = "ROBLOX 男孩右腿",
},
{
id = 376532000,
assetType = {
name = "躯干",
id = 27,
},
name = "ROBLOX 男孩躯干",
},
},
}
print(result)GetFriendsAsync
参数
代码示例
打印 Roblox 好友
local Players = game:GetService("Players")
local USERNAME = "Cozecant"
local function iterPageItems(pages)
return coroutine.wrap(function()
local pagenum = 1
while true do
for _, item in ipairs(pages:GetCurrentPage()) do
coroutine.yield(item, pagenum)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
pagenum = pagenum + 1
end
end)
end
-- 首先,获取玩家的用户 ID
local userId = Players:GetUserIdFromNameAsync(USERNAME)
-- 然后,为他们的好友获取一个 FriendPages 对象
local friendPages = Players:GetFriendsAsync(userId)
-- 遍历页面中的条目。对于 FriendPages,这些
-- 是关于好友的信息表,包括用户名。
-- 在一个表中收集每个用户名
local usernames = {}
for item, _pageNo in iterPageItems(friendPages) do
table.insert(usernames, item.Username)
end
print("连接的好友: " .. USERNAME .. ": " .. table.concat(usernames, ", "))GetHumanoidDescriptionFromOutfitId
GetHumanoidDescriptionFromOutfitIdAsync
参数
代码示例
从服装ID获取HumanoidDescription
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local function getOutfitId(bundleId)
if bundleId <= 0 then
return
end
local info = game.AssetService:GetBundleDetailsAsync(bundleId)
if not info then
return
end
for _, item in pairs(info.Items) do
if item.Type == "UserOutfit" then
return item.Id
end
end
return nil
end
local function getHumanoidDescriptionBundle(bundleId)
local itemId = getOutfitId(bundleId)
if itemId and itemId > 0 then
return Players:GetHumanoidDescriptionFromOutfitIdAsync(itemId)
end
return nil
end
local humanoidDescription = getHumanoidDescriptionBundle(799)
local humanoidModel = Players:CreateHumanoidModelFromDescriptionAsync(humanoidDescription, Enum.HumanoidRigType.R15)
humanoidModel.Parent = WorkspaceGetHumanoidDescriptionFromUserId
GetHumanoidDescriptionFromUserIdAsync
参数
代码示例
从用户 ID 获取 HumanoidDescription
game.Players:CreateHumanoidModelFromDescriptionAsync(
game.Players:GetHumanoidDescriptionFromUserIdAsync(1),
Enum.HumanoidRigType.R15
).Parent =
game.WorkspaceGetNameFromUserIdAsync
参数
返回
代码示例
从用户ID获取名称
local Players = game:GetService("Players")
-- 示例数据:
-- 用户ID: 118271 名称: "RobloxRulez"
-- 用户ID: 131963979 名称: "docsRule"
local nameOne = Players:GetNameFromUserIdAsync(118271)
local nameTwo = Players:GetNameFromUserIdAsync(131963979)
print(nameOne, nameTwo)
-- 输出: "RobloxRulez docsRule"从 UserId 获取名称 (使用缓存)
local Players = game:GetService("Players")
-- 创建一个名为 'cache' 的表来存储每个 'Name',当它们被找到时。
-- 如果我们使用相同的 'UserId' 查找一个 'Name',该 'Name' 将来自
-- 缓存(快速),而不是 GetNameFromUserIdAsync()(可yield)。
local cache = {}
function getNameFromUserId(userId)
-- 首先,检查缓存中是否包含 'userId'
local nameFromCache = cache[userId]
if nameFromCache then
-- 如果在缓存的键 'userId' 中存储了一个值,则该 'nameFromCache'
-- 是正确的名称,我们可以返回它。
return nameFromCache
end
-- 如果到这里,说明 'userId' 之前未被查找,且不存在于
-- 缓存中。现在我们需要使用 GetNameFromUserIdAsync() 来查找名称
local name
local success, _ = pcall(function()
name = Players:GetNameFromUserIdAsync(userId)
end)
if success then
-- 如果 'success' 为真,GetNameFromUserIdAsync() 成功找到了
-- 名称。使用 'userId' 作为键在缓存中存储这个名称,这样我们
-- 在未来就不需要再次查找这个名称。然后返回名称。
cache[userId] = name
return name
end
-- 如果到这里, 'success' 为假,意味着 GetNameFromUserIdAsync()
-- 无法找到提供的 'userId' 的 'name'。警告用户
-- 发生了这种情况,然后返回无,或 nil。
warn("无法找到 UserId 对应的名称:", userId)
return nil
end
-- 示例数据:
-- UserId: 118271 名称: "RobloxRulez"
-- UserId: 131963979 名称: "docsRule"
-- 第一次使用 UserId 时,将调用 GetNameFromUserIdAsync()
local nameOne = getNameFromUserId(118271)
local nameTwo = getNameFromUserId(131963979)
-- 由于 118271 已经被使用,将从缓存中获取它的名称
local nameOneQuick = getNameFromUserId(118271)
print(nameOne, nameTwo, nameOneQuick)
-- 输出: "RobloxRulez docsRule RobloxRulez"GetPlayerByUserId
GetPlayerFromCharacter
参数
返回
代码示例
玩家:从角色获取玩家
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local PLAYER_NAME = "Nightriff"
local character = Workspace:FindFirstChild(PLAYER_NAME)
local player = Players:GetPlayerFromCharacter(character)
if player then
print(`玩家 {player.Name} ({player.UserId}) 在游戏中`)
else
print(`玩家 {PLAYER_NAME} 不在游戏中!`)
endGetPlayers
返回
{Player}
代码示例
给每个人添加闪光
local Players = game:GetService("Players")
local function onCharacterAdded(character)
-- 如果他们的头上还没有闪光,则给他们添加闪光
if not character:FindFirstChild("Sparkles") then
local sparkles = Instance.new("Sparkles")
sparkles.Parent = character:WaitForChild("Head")
end
end
local function onPlayerAdded(player)
-- 检查他们是否已经生成
if player.Character then
onCharacterAdded(player.Character)
end
-- 监听玩家(重新)生成
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)getPlayers
GetUserIdFromNameAsync
参数
返回
代码示例
从名称获取用户ID
local Players = game:GetService("Players")
-- 示例数据:
-- 用户ID: 118271 名称: "RobloxRulez"
-- 用户ID: 131963979 名称: "docsRule"
local userIdOne = Players:GetUserIdFromNameAsync("RobloxRulez")
local userIdTwo = Players:GetUserIdFromNameAsync("docsRule")
print(userIdOne, userIdTwo)
-- 打印: "118271 131963979"从名称获取用户Id(使用缓存)
local Players = game:GetService("Players")
-- 创建一个名为 'cache' 的表格来存储每个找到的 'UserId'。
-- 如果我们用相同的 'Name' 查找 'UserId',那么 'UserId' 将来自
-- 缓存(快速),而不是 GetUserIdFromNameAsync() (异步)。
local cache = {}
function getUserIdFromName(name)
-- 首先,检查缓存是否包含 'name'
local userIdFromCache = cache[name]
if userIdFromCache then
-- 如果缓存中在键 'name' 处存储了一个值,那么这个 'userIdFromCache'
-- 就是正确的 UserId,我们可以返回它。
return userIdFromCache
end
-- 如果来到这里,说明 'name' 之前没有被查找过,并且在缓存中不存在。
-- 现在我们需要使用 GetUserIdFromNameAsync() 来查找 userId
local userId
local success, _ = pcall(function()
userId = Players:GetUserIdFromNameAsync(name)
end)
if success then
-- 如果 'success' 为 true,GetUserIdFromNameAsync() 成功找到了
-- userId。使用 'name' 作为键将这个 userId 存储在缓存中,这样我们
-- 就不用在未来查找这个 userId。然后返回 userId。
cache[name] = userId
return userId
end
-- 如果来到这里,说明 'success' 为 false,意味着 GetUserIdFromNameAsync()
-- 未能为提供的 'name' 找到 'userId'。我们可以提醒用户发生了这种情况,
-- 然后返回无值或 nil。
warn("无法找到名称的 UserId:", name)
return nil
end
-- 示例数据:
-- UserId: 118271 Name: "RobloxRulez"
-- UserId: 131963979 Name: "docsRule"
-- 第一次使用名称时,将调用 GetUserIdFromNameAsync()
local userIdOne = getUserIdFromName("RobloxRulez")
local userIdTwo = getUserIdFromName("docsRule")
-- 因为 "RobloxRulez" 之前已经使用过,所以从缓存中获取它的 UserId
local userIdOneQuick = getUserIdFromName("RobloxRulez")
print(userIdOne, userIdTwo, userIdOneQuick)
-- 打印: "118271 131963979 118271"GetUserThumbnailAsync
Players:GetUserThumbnailAsync(
参数
返回
代码示例
显示玩家缩略图
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local PLACEHOLDER_IMAGE = "rbxassetid://0" -- 替换为占位符图像
-- 获取缩略图
local userId = player.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
-- 将 ImageLabel 的内容设置为用户缩略图
local imageLabel = script.Parent
imageLabel.Image = (isReady and content) or PLACEHOLDER_IMAGE
imageLabel.Size = UDim2.new(0, 420, 0, 420)playerFromCharacter
players
SetChatStyle
参数
| 默认值:"Classic" |
返回
()
代码示例
设置玩家的聊天风格
-- 命令栏
game.Players:SetChatStyle(Enum.ChatStyle.Classic) -- 将聊天风格设置为经典
-- LocalScript
local Players = game:GetService("Players")
Players:SetChatStyle(Enum.ChatStyle.Classic) -- 出错TeamChat
UnbanAsync
参数
返回
()
代码示例
解封用户
local Players = game:GetService("Players")
if shouldBeUnbanned(player) then
local config: UnbanConfigType = {
UserIds = { player.UserId, 789 },
ApplyToUniverse = false,
}
local success, err = pcall(function()
return Players:UnbanAsync(config)
end)
print(success, err)
end活动
PlayerAdded
参数
代码示例
玩家.PlayerAdded
local Players = game:GetService("Players")
local function onPlayerAdded(player)
print("一个玩家已加入:" .. player.Name)
end
Players.PlayerAdded:Connect(onPlayerAdded)PlayerMembershipChanged
参数
代码示例
处理高级会员变更
local Players = game:GetService("Players")
local function grantPremiumBenefits(player)
-- 授予玩家访问仅限高级用户的区域、物品或任何您能想象的东西!
print("给予", player, "高级福利!")
end
local function playerAdded(player)
if player.MembershipType == Enum.MembershipType.Premium then
grantPremiumBenefits(player)
end
end
local function playerMembershipChanged(player)
print("收到事件 PlayerMembershipChanged。新会员资格 = " .. tostring(player.MembershipType))
if player.MembershipType == Enum.MembershipType.Premium then
grantPremiumBenefits(player)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerMembershipChanged:Connect(playerMembershipChanged)PlayerRemoving
参数
代码示例
玩家.玩家离开
local Players = game:GetService("Players")
local function onPlayerRemoving(player)
print("一个玩家已经离开:" .. player.Name)
end
Players.PlayerRemoving:Connect(onPlayerRemoving)UserSubscriptionStatusChanged