Players

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
บริการ

บริการ Players มีวัตถุ Player สำหรับลูกค้าที่เชื่อมต่ออยู่ในปัจจุบันกับเซิร์ฟเวอร์ Robloxนอกจากนี้ยังมีข้อมูลเกี่ยวกับการกำหนดค่าสถานที่มันสามารถดึงข้อมูลเกี่ยวกับผู้เล่นที่ไม่ได้เชื่อมต่อกับเซิร์ฟเวอร์ เช่น รูปลักษณ์ของตัวละคร เพื่อน และภาพรวมของอวตาร

สรุป

คุณสมบัติ

  • ไม่ซ้ำ
    ไม่สามารถเขียนสคริปต์
    อ่านพร้อมๆ กัน

    เปิดใช้งานหรือปิดใช้งานวิธีการสาม Players วิธี ( BanAsync() , UnbanAsync() และ GetBanHistoryAsync() ) ที่ประกอบขึ้นเป็น API การแบนคุณสมบัตินี้ไม่สามารถสคริปต์ได้และสามารถแก้ไขได้เฉพาะใน Studio

  • อ่านอย่างเดียว
    ไม่ซ้ำ
    อ่านพร้อมๆ กัน

    บ่งบอกว่าการแชทฟองอากาศถูกเปิดใช้งานหรือไม่ ตั้งค่าด้วยวิธี Players:SetChatStyle()

  • ไม่ซ้ำ
    อ่านพร้อมๆ กัน

    บ่งบอกว่า characters จะเกิดใหม่โดยอัตโนมัติหรือไม่

  • อ่านอย่างเดียว
    ไม่ซ้ำ
    อ่านพร้อมๆ กัน

    บ่งบอกว่าจะมีการเปิดใช้งานการแชทแบบคลาสสิกหรือไม่; ตั้งโดยวิธี Players:SetChatStyle()

  • อ่านอย่างเดียว
    ไม่ซ้ำ
    อ่านพร้อมๆ กัน

    The Player ที่ LocalScript กําลังทํางานอยู่

  • อ่านอย่างเดียว
    ไม่ซ้ำ
    อ่านพร้อมๆ กัน

    จํานวนผู้เล่นสูงสุดที่สามารถอยู่ในเซิร์ฟเวอร์ได้

  • อ่านอย่างเดียว
    ไม่ซ้ำ
    อ่านพร้อมๆ กัน

    จํานวนผู้เล่นที่ต้องการสําหรับเซิร์ฟเวอร์

  • อ่านพร้อมๆ กัน

    ควบคุมจำนวนเวลาที่ใช้สำหรับตัวละครผู้เล่นที่จะเกิดใหม่

  • ไม่สามารถเขียนสคริปต์
    อ่านพร้อมๆ กัน

วิธีการ

  • Chat(message : string):()
    การรักษาความปลอดภัยของปลั๊กอิน

    ทำให้ผู้เล่นท้องถิ่นแชทข้อความที่กำหนดไว้

  • เขียนพร้อมๆ กัน

    ส่งคืน Player ด้วย UserId ที่กำหนดไว้หากพวกเขาอยู่ในเกม

  • คืนค่า Player ซึ่ง Player.Character อินสแตนซ์nil หากไม่พบได้

  • GetPlayers():Instances
    เขียนพร้อมๆ กัน

    คืนตารางของวัตถุทั้งหมดที่เชื่อมต่ออยู่ในปัจจุบัน Player อยู่

  • การรักษาความปลอดภัยของปลั๊กอิน

    ตั้งค่าว่า BubbleChat และ ClassicChat จะถูกใช้หรือไม่ และบอก TeamChat และ Chat สิ่งที่ต้องทำ

  • TeamChat(message : string):()
    การรักษาความปลอดภัยของปลั๊กอิน

    ทำให้ผู้เล่นท้องถิ่นแชทข้อความที่กำหนด ซึ่งจะมองเห็นได้เฉพาะผู้ใช้ในทีมเดียวกันเท่านั้น

  • BanAsync(config : Dictionary):()
    ผลตอบแทน

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

  • ผลตอบแทน

    ส่งคืนโมเดลตัวละครที่ติดตั้งทุกอย่างตามที่ระบุไว้ใน HumanoidDescription และเป็น R6 หรือ R15 ตามที่กำหนดโดย rigType

  • ผลตอบแทน

    คืนชุดตั้งค่าตัวละครที่มีทุกอย่างพร้อมสำหรับการจับคู่กับอวตารของผู้ใช้ที่ระบุโดยใช้ userId ที่ส่ง

  • ผลตอบแทน

    ดึงประวัติการแบนและยกเลิกการแบนของผู้ใช้ใดๆ ภายในจักรวาลประสบการณ์วิธีนี้เปิดใช้งานและปิดใช้งานโดยคุณสมบัติ Players.BanningEnabled ซึ่งคุณสามารถสลับได้ใน Studio

  • ผลตอบแทน

    ส่งคืนข้อมูลเกี่ยวกับรูปลักษณ์ของตัวละครของผู้ใช้ที่กำหนด

  • ผลตอบแทน

    ส่งคืนวัตถุ FriendPages ที่มีข้อมูลสำหรับเพื่อนของผู้เล่นทั้งหมดที่กำหนด

  • ส่งคืนคำอธิบายมนุษย์สำหรับชุดที่กำหนดซึ่งจะถูกตั้งค่าด้วยส่วน/สี/แอนิเมชั่นต่างๆของชุด

  • ส่งคืนคำอธิบายมนุษย์ที่ระบุถึงทุกอย่างที่ติดตั้งสำหรับอวตารของผู้ใช้ที่ระบุโดย userId ที่ส่ง

  • ผลตอบแทน

    ส่งคำถามไปยังเว็บไซต์ Roblox สําหรับชื่อผู้ใช้ของบัญชีที่มี UserId ที่กำหนด

  • ผลตอบแทน

    ส่งคำถามไปยังเว็บไซต์ Roblox สําหรับ userId ของบัญชีที่มีชื่อผู้ใช้ที่กำหนด

  • GetUserThumbnailAsync(userId : number,thumbnailType : Enum.ThumbnailType,thumbnailSize : Enum.ThumbnailSize):Tuple
    ผลตอบแทน

    คืน URL พิมพ์รวมถึงบูลีนที่อธิบายถึงว่าภาพพร้อมใช้งานหรือไม่

  • UnbanAsync(config : Dictionary):()
    ผลตอบแทน

    ยกเลิกการแบนผู้เล่นที่ถูกแบนจาก Players:BanAsync() หรือ Cloud APIPlayers.BanningEnabled ซึ่งคุณสามารถสลับได้ใน Studio

อีเวนต์

  • เกิดไฟไหม้เมื่อผู้เล่นเข้าสู่เกม

  • ไฟเมื่อเซิร์ฟเวอร์เกมรู้ว่าสมาชิกของผู้เล่นเปลี่ยนแปลงแล้ว

  • เกิดไฟไหม้เมื่อผู้เล่นกำลังจะออกจากเกม

  • เกิดไฟไหม้เมื่อเซิร์ฟเวอร์เกมรู้ว่าสถานะของผู้ใช้สำหรับสมัครสมาชิกบางอย่างได้เปลี่ยนแปลง

คุณสมบัติ

BanningEnabled

ไม่ซ้ำ
ไม่สามารถเขียนสคริปต์
อ่านพร้อมๆ กัน

เปิดใช้งานหรือปิดใช้งานวิธีการสาม Players วิธี ( BanAsync() , UnbanAsync() และ GetBanHistoryAsync() ) ที่ประกอบขึ้นเป็น API การแบนคุณสมบัตินี้ไม่สามารถสคริปต์ได้และสามารถแก้ไขได้เฉพาะใน Studio

BubbleChat

อ่านอย่างเดียว
ไม่ซ้ำ
อ่านพร้อมๆ กัน

คุณสมบัตินี้บ่งบอกว่ามีการเปิดใช้งานการแชทฟองอากาศหรือไม่ มันถูกตั้งค่าด้วยวิธี Players:SetChatStyle() โดยใช้เอนเนม Enum.ChatStyle

เมื่อโหมดแชทนี้เปิดใช้งานแล้ว เกมจะแสดงการแชทในอินเทอร์เฟซผู้ใช้แชทที่มุมซ้ายบนของหน้าจอ

มีสองโหมดแชทอื่น ๆ คือ Players.ClassicChat และโหมดแชทที่เปิดใช้งานทั้งคลาสสิกและฟองสบู่

CharacterAutoLoads

ไม่ซ้ำ
อ่านพร้อมๆ กัน

คุณสมบัตินี้บ่งบอกว่า characters จะเกิดใหม่โดยอัตโนมัติหรือไม่ ค่าเริ่มต้นคือจริง

หากคุณสมบัตินี้ถูกปิดใช้งาน (ปิด) ผู้เล่น characters จะไม่เกิดจนกว่าฟังก์ชัน Player:LoadCharacter() จะถูกเรียกสำหรับแต่ละ Player รวมถึงเมื่อผู้เล่นเข้าร่วมประสบการณ์

สิ่งนี้สามารถมีประโยชน์ในประสบการณ์ที่ผู้เล่นมีชีวิตจำกัด เช่น เกมแข่งขันที่ผู้เล่นไม่สามารถฟื้นฟูได้จนกว่ารอบเกมจะสิ้นสุด

ตัวอย่างโค้ด

This example demonstrates one possible usage of the Players.CharacterAutoLoads property.

The example below respawns all players in the game, if dead, once every 10 seconds. This means that players who die 1 second after all players respawn must wait 9 seconds until the script loads all Player.Character again.

First, this script removes a player's character when they die and the Humanoid.Died function fires. This is done so that the respawn loop that executes every 10 seconds reloads that player when it does not find the player's character in the Workspace.

To work as expected, this example should be run within a Script.

Player Respawn Timer

local Players = game:GetService("Players")
-- Set CharacterAutoLoads to false
Players.CharacterAutoLoads = false
-- Remove player's character from workspace on death
Players.PlayerAdded:Connect(function(player)
while true do
local char = player.CharacterAdded:Wait()
char.Humanoid.Died:Connect(function()
char:Destroy()
end)
end
end)
-- Respawn all dead players once every 10 seconds
while true do
local players = Players:GetChildren()
-- Check if each player is dead by checking if they have no character, if dead load that player's character
for _, player in pairs(players) do
if not workspace:FindFirstChild(player.Name) then
player:LoadCharacter()
end
end
-- Wait 10 seconds until next respawn check
task.wait(10)
end

ClassicChat

อ่านอย่างเดียว
ไม่ซ้ำ
อ่านพร้อมๆ กัน

บ่งบอกว่ามีการเปิดใช้งานการแชทแบบคลาสสิกหรือไม่ คุณสมบัตินี้ถูกกำหนดโดยวิธี Players:SetChatStyle() โดยใช้ enum Enum.ChatStyle

เมื่อโหมดแชทนี้เปิดใช้งานแล้ว เกมจะแสดงการแชทในฟองอากาศเหนือหัวของผู้ส่ง

มีสองโหมดแชทอื่น ๆ คือ Players.BubbleChat และโหมดแชทที่เปิดใช้งานทั้งคลาสสิกและฟองสบู่

LocalPlayer

อ่านอย่างเดียว
ไม่ซ้ำ
อ่านพร้อมๆ กัน

คุณสมบัติอ่านเฉพาะนี้อ้างถึง Player ที่ลูกค้ากำลังดำเนินการประสบการณ์

คุณสมบัตินี้จะถูกกำหนดเฉพาะสำหรับ LocalScripts และ ModuleScripts รหัสScript ที่เรียกใช้โค้ดของพวกเขาทำงาน คุณสมบัตินี้คือ nil

MaxPlayers

อ่านอย่างเดียว
ไม่ซ้ำ
อ่านพร้อมๆ กัน

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

PreferredPlayers

อ่านอย่างเดียว
ไม่ซ้ำ
อ่านพร้อมๆ กัน

คุณสมบัตินี้บ่งบอกถึงจํานวนผู้เล่นที่ matchmaker ของ Roblox จะเติมเซิร์ฟเวอร์หมายเลขนี้จะน้อยกว่าจํานวนผู้เล่นสูงสุด ( Players.MaxPlayers ) ที่สนับสนุนโดยประสบการณ์

RespawnTime

อ่านพร้อมๆ กัน

คุณสมบัตินี้ควบคุมเวลาในวินาทีที่ใช้เวลาสำหรับผู้เล่นในการเกิดใหม่เมื่อ Players.CharacterAutoLoads เป็นจริง มันจะเริ่มต้นที่ 5.0 วินาที

นี่มีประโยชน์เมื่อคุณต้องการเปลี่ยนระยะเวลาในการเกิดใหม่ขึ้นอยู่กับประเภทประสบการณ์ของคุณ แต่ไม่ต้องการจัดการการเกิดของผู้เล่นแต่ละคน

แม้ว่าคุณสมบัตินี้จะสามารถตั้งค่าได้จากภายใน Script แต่คุณสามารถตั้งค่าได้ง่ายขึ้นโดยตรงบนวัตถุ Players ในหน้าต่าง Explorer ของ Studio

UseStrafingAnimations

ไม่สามารถเขียนสคริปต์
อ่านพร้อมๆ กัน

วิธีการ

Chat

()
การรักษาความปลอดภัยของปลั๊กอิน

ฟังก์ชันนี้ทำให้ผู้เล่นท้องถิ่นแชทข้อความที่กำหนดไว้เนื่องจากไอเทมนี้ได้รับการคุ้มครองแล้ว การพยายามใช้งานใน Script หรือ LocalScript จะทำให้เกิดข้อผิดพลาด

แทนที่จะสร้างระบบแชทที่กําหนดเองหรือระบบที่ต้องการการเข้าถึงแชท คุณสามารถใช้ฟังก์ชัน Chat ของบริการ Chat:Chat() แทนได้

พารามิเตอร์

message: string

ข้อความแชท

ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

This example demonstrates that the Players:Chat() function executes without error if using the Command Bar or a Plugin (assuming the local player can chat freely) and errors if executed in a Script.

Players:Chat

-- Command bar
game:GetService("Players"):Chat("Hello, world!") --Results in 'Hello, world!' appearing in the Chat log under your Player's name.
-- Script
local Players = game:GetService("Players")
Players:Chat("Hello, world!") --Errors

GetPlayerByUserId

เขียนพร้อมๆ กัน

ฟังก์ชันนี้ค้นหาแต่ละ Player ใน Players สำหรับหนึ่งที่มี Player.UserId ตรงกับที่กำหนดไว้ userIdหากผู้เล่นดังกล่าวไม่มีอยู่ มันจะคืน nil

วิธีนี้มีประโยชน์ในการค้นหาผู้ซื้อผลิตภัณฑ์นักพัฒนาโดยใช้ MarketplaceService.ProcessReceipt ซึ่งให้ตารางที่รวมถึงผู้ซื้อ UserId และไม่ใช่การอ้างอิงถึงวัตถุ Player เองประสบการณ์ส่วนใหญ่จะต้องมีการอ้างอิงถึงผู้เล่นเพื่อให้ผลิตภัณฑ์

พารามิเตอร์

userId: number

The Player.UserId ของผู้เล่นที่ระบุไว้

ค่าเริ่มต้น: ""

ส่งค่ากลับ

ตัวอย่างโค้ด

Players:GetPlayerByUserId

local Players = game:GetService("Players")
local player = Players:GetPlayerByUserId(1)
if player then
print("Player with userId 1 is in this server! Their name is: " .. player.Name)
else
print("Player with userId 1 is not in this server!")
end

GetPlayerFromCharacter

ฟังก์ชันนี้คืนค่า Player ที่เกี่ยวข้องกับ Player.Character ที่กำหนดหรือ nil หากไม่พบได้เทียบเท่ากับฟังก์ชันต่อไปนี้:


local function getPlayerFromCharacter(character)
for _, player in game:GetService("Players"):GetPlayers() do
if player.Character == character then
return player
end
end
end

วิธีนี้มักใช้เมื่อเกิดเหตุการณ์บางอย่างในตัวละครของผู้เล่นจะลุกเป็นไฟ (เช่น Class.Humanoid``Class.Humanoid.Died|dying )อีเวนต์ดังกล่าวอาจไม่ได้อ้างอิงถึงวัตถุผู้เล่นโดยตรง แต่วิธีนี้ให้การเข้าถึงง่ายอันตรายของฟังก์ชันนี้สามารถอธิบายได้ว่าเป็นการรับตัวละครของผู้เล่นเพื่อทำเช่นนี้ เพียงแค่เข้าถึงคุณสมบัติของตัวละคร

พารามิเตอร์

character: Model

ตัวอย่างตัวละครที่คุณต้องการรับผู้เล่นจาก

ค่าเริ่มต้น: ""

ส่งค่ากลับ

ตัวอย่างโค้ด

Players:GetPlayerFromCharacter

Players: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 {player.Name} ({player.UserId}) is in the game`)
else
print(`Player {PLAYER_NAME} is not in the game!`)
end

GetPlayers

Instances
เขียนพร้อมๆ กัน

วิธีนี้จะคืนตารางของวัตถุทั้งหมดที่เชื่อมต่ออยู่ในปัจจุบัน Player อยู่มันทำงานในลักษณะเดียวกัน Instance:GetChildren() จะยกเว้นที่มันส่งคืนเฉพาะวัตถุ Player ที่พบภายใต้ Playersเมื่อใช้กับลูป for จะมีประโยชน์ในการเลื่อนผ่านผู้เล่นทั้งหมดในเกม


local Players = game:GetService("Players")
for _, player in Players:GetPlayers() do
print(player.Name)
end

สคริปต์ที่เชื่อมต่อกับ Players.PlayerAdded มักจะพยายามประมวลผลผู้เล่นทุกคนที่เชื่อมต่อกับเกมวิธีนี้มีประโยชน์สำหรับการเลื่อนผ่านผู้เล่นที่เชื่อมต่อแล้วที่จะไม่ยิง PlayerAddedการใช้วิธีนี้ช่วยให้แน่ใจว่าไม่มีผู้เล่นคนใดถูกข้าม!


local Players = game:GetService("Players")
local function onPlayerAdded(player)
print("Player: " .. player.Name)
end
for _, player in Players:GetPlayers() do
onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

ส่งค่ากลับ

Instances

ตารางที่มีผู้เล่นทั้งหมดในเซิร์ฟเวอร์

ตัวอย่างโค้ด

This code sample listens for players spawning and gives them Sparkles in their head. It does this by defining two functions, onPlayerSpawned and onPlayerAdded.

Give Sparkles to Everyone

local Players = game:GetService("Players")
local function onCharacterAdded(character)
-- Give them sparkles on their head if they don't have them yet
if not character:FindFirstChild("Sparkles") then
local sparkles = Instance.new("Sparkles")
sparkles.Parent = character:WaitForChild("Head")
end
end
local function onPlayerAdded(player)
-- Check if they already spawned in
if player.Character then
onCharacterAdded(player.Character)
end
-- Listen for the player (re)spawning
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)

SetChatStyle

()
การรักษาความปลอดภัยของปลั๊กอิน

ฟังก์ชันนี้ตั้งค่าว่า BubbleChat และ ClassicChat จะถูกใช้หรือไม่ และบอก TeamChat และ Chat ว่าควรทำอย่างไรโดยใช้ enum Enum.ChatStyleเนื่องจากไอเทมนี้ได้รับการคุ้มครองแล้ว การพยายามใช้งานใน Script หรือ LocalScript จะทำให้เกิดข้อผิดพลาด

ฟังก์ชันนี้ใช้ภายในเมื่อโหมดแชทถูกตั้งค่าโดยเกม

พารามิเตอร์

สไตล์การแชทที่กำหนดไว้ถูกตั้งค่า

ค่าเริ่มต้น: "Classic"

ส่งค่ากลับ

()

ตัวอย่างโค้ด

This example demonstrates that the Players:SetChatStyle() function executes without error if using the Command Bar or a Plugin and errors if executed in a LocalScript.

When executed in the Command Bar, this code sets the chat style to Classic using the Enum.ChatStyle enum.

Setting a Player's Chat Style

-- Command bar
game.Players:SetChatStyle(Enum.ChatStyle.Classic) -- Set's chat style to Classic
-- LocalScript
local Players = game:GetService("Players")
Players:SetChatStyle(Enum.ChatStyle.Classic) -- Errors

TeamChat

()
การรักษาความปลอดภัยของปลั๊กอิน

ฟังก์ชันนี้ทำให้การแชท Players.LocalPlayer ของข้อความที่กำหนดให้มองเห็นได้เฉพาะผู้ใช้ในทีมเดียวกันเท่านั้นเนื่องจากไอเทมนี้ได้รับการคุ้มครองแล้ว การพยายามใช้งานใน Script หรือ LocalScript จะทำให้เกิดข้อผิดพลาด

ฟังก์ชันนี้ใช้ภายในเมื่อ Players.LocalPlayer ส่งข้อความไปยังทีมของพวกเขา

พารามิเตอร์

message: string

ข้อความที่กำลังแชท

ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

This example demonstrates that the Players:TeamChat() function executes without error if using the Command Bar or a Plugin and errors if executed in a LocalScript.

When executed in the Command Bar, the function sends the specified message to all players on the same Team as the Players.LocalPlayer.

Sending Team Chat

-- Command bar
game.Players:TeamChat("Hello World") -- Sends a "Hello World" message to all players on the local player's team
-- LocalScript
local Players = game:GetService("Players")
Players:TeamChat("Hello World") -- Errors

BanAsync

()
ผลตอบแทน

วิธี Players:BanAsync() ช่วยให้คุณสามารถแบนผู้ใช้ที่ละเมิดข้อกําหนดด้านประสบการณ์ของคุณได้อย่างง่ายดายคุณสามารถระบุระยะเวลาการแบน, เปิดใช้งานการแบนเพื่อแพร่กระจายไปยังบัญชีทดแทนที่น่าสงสัย และให้ข้อความแก่ผู้ใช้ที่ถูกแบนตามข้อกําหนดการใช้งาน คําแนะนําคุณควรโพสต์กฎประสบการณ์ของคุณที่สามารถเข้าถึงได้ทุกที่สำหรับผู้ใช้ทั้งหมดและให้วิธีสำหรับพวกเขาในการอุทธรณ์วิธีนี้เปิดใช้งานและปิดใช้งานโดยคุณสมบัติ Players.BanningEnabled ซึ่งคุณสามารถสลับได้ใน Studio

การแบนและการส่งข้อความ

ผู้ใช้ที่ถูกแบนจะถูกนำออกทันทีและป้องกันไม่ให้เข้าร่วมประสบการณ์ของคุณอีกครั้งพวกเขาจะได้รับการนำเสนอด้วยโมดัลข้อผิดพลาดที่แสดงเวลาที่เหลือบนการแบนของพวกเขาและ DisplayReason ของคุณระบบเซิร์ฟเวอร์ด้านหลังของ Roblox จะไล่ผู้เล่นออกจากทุกเซิร์ฟเวอร์ตามสถานที่ที่คุณระบุDisplayReason สามารถมีความยาวสูงสุด 400 ตัวอักษรและอยู่ภายใต้การกรองข้อความสำหรับข้อมูลเพิ่มเติมเกี่ยวกับข้อความโมดัลที่ยอมรับได้ ดู ห้ามการส่งข้อความ

สถานที่และจักรวาล

โดยค่าเริ่มต้นแบนจะขยายไปยังสถานที่ใดก็ได้ภายในจักรวาลนั้นเพื่อจำกัดการแบนเฉพาะสถานที่ที่ API นี้ถูกเรียกจากเท่านั้น ให้กำหนดค่า ApplyToUniverse เป็น falseอย่างไรก็ตาม หากผู้ใช้ถูกแบนในสถานที่เริ่มต้นของจักรวาล มันจะส่งผลให้ผู้ใช้ถูกยกเว้นออกจากทั้งหมดของจักรวาล ไม่ว่าจะมีการแบนทั่วไปหรือไม่ก็ตาม

บัญชีทางเลือก

ผู้ใช้มักจะเล่นภายใต้หลายบัญชีที่แตกต่างกันซึ่งเรียกว่าบัญชีสำรองหรือบัญชี alt ซึ่งบางครั้งใช้เพื่อหลีกเลี่ยงการแบนบัญชีเพื่อช่วยให้คุณป้องกันผู้ใช้ที่ถูกแบนออกไป พฤติกรรมเริ่มต้นของ API นี้จะแพร่กระจายการแบนทั้งหมดจากบัญชีที่คุณแบนไปยังบัญชี alt ที่น่าสงสัยใดๆคุณสามารถปิดการเผยแพร่การแบนไปยังบัญชีอื่นโดยการกำหนดค่า ExcludeAltAccounts เป็น true

ระยะเวลาการแบน

การละเมิดทั้งหมดไม่เหมือนกัน ดังนั้นการห้ามทั้งหมดจึงไม่ควรมีความยาวเท่ากันAPI นี้ช่วยให้คุณกำหนดระยะเวลาของการแบนในเวลาไม่กี่วินาทีด้วยฟิลด์ Durationเพื่อกำหนดการแบนถาวร ตั้งฟิลด์เป็น -1คุณอาจต้องการกำหนดค่าระยะเวลาการแบนไดนามิกตามประวัติการแบนของผู้ใช้ซึ่งคุณสามารถสอบถามได้โดยใช้ Players:GetBanHistoryAsync()ตัวอย่างเช่นคุณอาจต้องการพิจารณาจํานวนการแบน, ระยะเวลาของการแบนก่อนหน้านี้หรือสร้างโลจิสติกจากบันทึกที่คุณบันทึกภายใต้ PrivateReason ซึ่งสามารถมีความยาวได้ถึง 1000 ตัวอักษรและไม่ได้ถูกกรองด้วยข้อความPrivateReason โน้ตไม่เคยถูกแบ่งปันกับไคลเอนต์และสามารถพิจารณาว่าปลอดภัยจากผู้โจมตีได้

ข้อผิดพลาดและการจำกัดความเร็ว

วิธีนี้เรียกการโทร HTTP ไปยังบริการด้านหลังที่อาจถูกจำกัดและอาจล้มเหลวหากคุณเรียก API นี้ด้วยมากกว่าหนึ่ง UserId วิธีนี้จะพยายามทำการโทร HTTP สำหรับแต่ละรหัสจากนั้นจะรวบรวมข้อความข้อผิดพลาดและรวมเข้ากับพวกเขาเป็นรายการแยกกับจุลภาคตัวอย่างเช่น หากวิธีนี้ถูกเรียกใช้สำหรับผู้ใช้ห้ารายและคำขอสำหรับผู้ที่มี UserIds 2 และ 4 ล้มเหลว ข้อความข้อผิดพลาดต่อไปนี้จะปรากฏขึ้น:

HTTP failure for UserId 2: Timedout, HTTP 504 (Service unavailable) failure for UserId 4: Service exception

ข้อความจะมีเสมอ failure for UserId {} หากเป็นข้อผิดพลาด HTTP

ความต้องการด้านไคลเอนต์

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

API นี้ใช้ ข้อจํากัดการใช้งานของผู้ใช้เปิด API เมฆ คุณจะสามารถใช้ API เหล่านี้เพื่อจัดการการแบนของคุณในแอปพลิเคชันบุคคลที่สามได้

พารามิเตอร์

config: Dictionary
  • UserIds (จำเป็น; รายการ) — รายการของ UserIds ผู้เล่นที่จะถูกแบน ขนาดสูงสุดคือ 50

  • ApplyToUniverse (เลือกได้; เบื้องต้น) — ว่าการแบนจะแพร่กระจายไปยังทุกสถานที่ภายในจักรวาลประสบการณ์หรือไม่ ค่าเริ่มต้นคือ true

  • Duration (จําเป็น; เลขอตัน) — ระยะเวลาการแบน, ในวินาทีการแบนถาวรควรมีค่าเป็น -1 .0 และค่าลบอื่น ๆ ทั้งหมดไม่ถูกต้อง

  • DisplayReason (จำเป็น; สตริง) — ข้อความที่จะแสดงให้กับผู้ใช้เมื่อพวกเขาพยายามเข้าร่วมประสบการณ์และล้มเหลวความยาวสตริงสูงสุดคือ 400 .

  • PrivateReason (จำเป็น; สตริง) — การส่งข้อความภายในที่จะถูกส่งคืนเมื่อสอบถามประวัติการแบนของผู้ใช้ ความยาวสตริงสูงสุดคือ 1000

  • ExcludeAltAccounts (เลือกได้; เบื้องต้น) — เมื่อ true Roblox จะไม่พยายามห้ามบัญชี alt โดยปกติค่าเริ่มต้นคือ false

ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

The following example bans a user with a duration calculated from their ban history, scoped to the entire universe and all of the user's alternate accounts.

Banning Users

local Players = game:GetService("Players")
if shouldBeBanned(player) then
local banHistoryPages = Players:GetBanHistoryAsync(player.UserId)
local duration = getNextBanDuration(banHistoryPages) -- Creator-implemented logic
local config: BanConfigType = {
UserIds = { player.UserId },
Duration = duration,
DisplayReason = "You violated community guideline #5",
PrivateReason = "Put anything here that the user should not know but is helpful for your records",
ExcludeAltAccounts = false,
ApplyToUniverse = true,
}
local success, err = pcall(function()
return Players:BanAsync(config)
end)
print(success, err)
end

CreateHumanoidModelFromDescription

ผลตอบแทน

ส่งคืนโมเดลตัวละครที่ติดตั้งทุกอย่างตามที่ระบุไว้ใน HumanoidDescription และเป็น R6 หรือ R15 ตามที่กำหนดโดย rigType

พารามิเตอร์

description: HumanoidDescription

กำหนดรูปลักษณ์ของตัวละครที่ส่งคืน

ค่าเริ่มต้น: ""

ระบุว่าตัวละครที่ส่งคืนจะเป็น R6 หรือ R15

ค่าเริ่มต้น: ""
assetTypeVerification: Enum.AssetTypeVerification

การตรวจสอบประเภทสินทรัพย์จะตัดสินว่าฟังก์ชันนี้จะโหลดโมเดลหรือไม่ (คุณควรตั้งค่านี้เป็นเสมอยกเว้นถ้าคุณต้องการโหลดสินทรัพย์ที่ไม่ได้อยู่ในแคตตาล็อก)

ค่าเริ่มต้น: "Default"

ส่งค่ากลับ

โมเดลตัวละครฮิวมานอยด์

ตัวอย่างโค้ด

This code sample creates a Humanoid Model from the passed in HumanoidDescription and parents the Model to the Workspace.

Create Humanoid Model From Description

game.Players:CreateHumanoidModelFromDescription(Instance.new("HumanoidDescription"), Enum.HumanoidRigType.R15).Parent =
game.Workspace

CreateHumanoidModelFromUserId

ผลตอบแทน

คืนชุดตั้งค่าตัวละครที่มีทุกอย่างพร้อมสำหรับการจับคู่กับอวตารของผู้ใช้ที่ระบุโดยใช้ userId ที่ส่งซึ่งรวมถึงว่าตัวละครนั้นอยู่ในขณะนี้เป็น R6 หรือ R15

พารามิเตอร์

userId: number

ID สำหรับผู้ใช้ Roblox (UserId คือหมายเลขในโปรไฟล์ของผู้ใช้เช่น www.roblox.com/users/1/profile)

ค่าเริ่มต้น: ""

ส่งค่ากลับ

โมเดลตัวละครฮิวมานอยด์

ตัวอย่างโค้ด

This code sample creates a Humanoid Model to match the avatar of the passed in User ID, and parents the Model to the Workspace.

Create Humanoid Model From A User ID

game.Players:CreateHumanoidModelFromUserId(1).Parent = game.Workspace

GetBanHistoryAsync

ผลตอบแทน

ดึงประวัติการแบนและยกเลิกการแบนของผู้ใช้ใดๆ ภายในจักรวาลประสบการณ์วิธีนี้จะคืนตัวอย่าง BanHistoryPages ที่มีการมรดกจาก Pagesวิธีนี้เปิดใช้งานและปิดใช้งานโดยคุณสมบัติ Players.BanningEnabled ซึ่งคุณสามารถสลับได้ใน Studio

การเรียกฟังก์ชันนี้จะสําเร็จเฉพาะในเซิร์ฟเวอร์เกมการผลิตและไม่ใช่ในอุปกรณ์ลูกค้าหรือใน Studio

API นี้ใช้ ข้อจํากัดการใช้งานของผู้ใช้เปิด API เมฆ คุณจะสามารถใช้ API เหล่านี้เพื่อจัดการการแบนของคุณในแอปพลิเคชันบุคคลที่สามได้

พารามิเตอร์

userId: number
ค่าเริ่มต้น: ""

ส่งค่ากลับ

ดู BanHistoryPages สำหรับการอ้างอิงการคืน

GetCharacterAppearanceInfoAsync

ผลตอบแทน

ฟังก์ชันนี้ส่งคืนข้อมูลเกี่ยวกับอวตารของผู้เล่น (ไม่สนใจอุปกรณ์) บนเว็บไซต์ Roblox ในรูปแบบของสารานุกรมไม่ควรสับสนกับ GetCharacterAppearanceAsync ซึ่งจริงๆ โหลดสินทรัพย์ที่อธิบายโดยวิธีนี้คุณสามารถใช้ InsertService:LoadAsset() อวาตาร์:


<th>ประเภท</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tr>
<td><code>สินทรัพย์</code></td>
<td>ตาราง (ดูด้านล่าง)</td>
<td>อธิบายสินทรัพย์ที่ติดตั้ง (หมวก ส่วนของร่างกาย เป็นต้น)</td>
</tr>
<tr>
<td><code>สีผิวบอดี้</code></td>
<td>ตาราง (ดูด้านล่าง)</td>
<td>อธิบายค่าสี BrickColor สำหรับแต่ละขา</td>
</tr>
<tr>
<td><code>สีผิว3s</code></td>
<td>ตาราง (ดูด้านล่าง)</td>
<td>อธิบายตัวอย่างสี 3 สำหรับแต่ละขาที่อาจไม่ตรงกับสีผิวของร่างกาย</td>
</tr>
<tr>
<td><code>กางเกงเริ่มต้นที่ใช้</code></td>
<td>บูล</td>
<td>อธิบายว่ากางเกงเริ่มต้นถูกใช้หรือไม่</td>
</tr>
<tr>
<td><code>เสื้อยืดเริ่มต้นที่ใช้</code></td>
<td>บูล</td>
<td>อธิบายว่าเสื้อยืดเริ่มต้นถูกใช้หรือไม่</td>
</tr>
<tr>
<td><code>อีโมท</code></td>
<td>ตาราง (ดูด้านล่าง)</td>
<td>อธิบายการแสดงอารมณ์ที่ติดตั้งแล้ว</td>
</tr>
<tr>
<td><code>ประเภทตัวละครผู้เล่น</code></td>
<td>สตริง</td>
<td>ทั้ง "R15" หรือ "R6"</td>
</tr>
<tr>
<td><code>ขนาด</code></td>
<td>ตาราง (ดูด้านล่าง)</td>
<td>อธิบายปัจจัยการปรับขนาดร่างกายต่างๆ</td>
</tr>
ชื่อ
ตารางย่อยของสินทรัพย์

ตาราง assets เป็นคอลเลกชันของตารางที่มีกุญแจต่อไปนี้ที่อธิบายสินทรัพย์ที่ติดตั้งอยู่ในปัจจุบันโดยผู้เล่น:


<th>ประเภท</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tr>
<td><code>id</code></td>
<td>จํานวน</td>
<td>รหัสสินทรัพย์ของสินทรัพย์ที่ติดตั้ง</td>
</tr>
<tr>
<td><code>ประเภทสินทรัพย์</code></td>
<td>ตาราง</td>
<td>ตารางที่มีชื่อ และฟิลด์ id แต่ละฟิลด์อธิบายประเภทของสินทรัพย์ที่ติดตั้ง ("หมวก", "ใบหน้า" เป็นต้น)</td>
</tr>
<tr>
<td><code>ชื่อ</code></td>
<td>สตริง</td>
<td>ชื่อของสินทรัพย์ที่ติดตั้ง</td>
</tr>
ชื่อ
ตารางย่อยของเครื่องชั่ง

ตาราง scales มีกุญแจต่อไปนี้ซึ่งเป็นตัวเลขที่ตรงกับค่าการขยายตัวหนึ่ง Humanoid : bodyType , head , height , proportion , depth , width .

สีร่างกายย่อยโต๊ะ

ตาราง bodyColors มีกุญแจต่อไปนี้แต่ละกุญแจตรงกับหมายเลข BrickColor ID ที่สามารถใช้กับ BrickColor.new(id) : leftArmColorId , torsoColorId , rightArmColorId , headColorId , leftLegColorId , rightLegColorId .

พารามิเตอร์

userId: number

ID * ของผู้เล่นที่ระบุ

ค่าเริ่มต้น: ""

ส่งค่ากลับ

สารานุกรมที่มีข้อมูลเกี่ยวกับรูปลักษณ์ของตัวละครของผู้ใช้ที่กำหนดไว้

ตัวอย่างโค้ด

Sometimes it is best to see an example of the returned dictionary structure in pure Lua. Here is one such example of a player whose avatar uses a package and wears several hats. Can you guess who it is?

Example Return Character Appearance Dictionary

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 = "Striped Hat",
},
{
id = 13062491,
assetType = {
name = "Face Accessory",
id = 42,
},
name = "Vision Française ",
},
{
id = 16598440,
assetType = {
name = "Neck Accessory",
id = 43,
},
name = "Red Bow Tie",
},
{
id = 28999228,
assetType = {
name = "Face",
id = 18,
},
name = "Joyous Surprise",
},
{
id = 86896488,
assetType = {
name = "Shirt",
id = 11,
},
name = "Expensive Red Tuxedo Jacket",
},
{
id = 86896502,
assetType = {
name = "Pants",
id = 12,
},
name = "Expensive Red Tuxedo Pants",
},
{
id = 376530220,
assetType = {
name = "Left Arm",
id = 29,
},
name = "ROBLOX Boy Left Arm",
},
{
id = 376531012,
assetType = {
name = "Right Arm",
id = 28,
},
name = "ROBLOX Boy Right Arm",
},
{
id = 376531300,
assetType = {
name = "Left Leg",
id = 30,
},
name = "ROBLOX Boy Left Leg",
},
{
id = 376531703,
assetType = {
name = "Right Leg",
id = 31,
},
name = "ROBLOX Boy Right Leg",
},
{
id = 376532000,
assetType = {
name = "Torso",
id = 27,
},
name = "ROBLOX Boy Torso",
},
},
}
print(result)

GetFriendsAsync

ผลตอบแทน

ฟังก์ชัน GetFriends Players จะคืนวัตถุ FriendPages ซึ่งมีข้อมูลสำหรับเพื่อนของผู้ใช้ทั้งหมดที่กำหนดรายการภายในวัตถุ FriendPages เป็นตารางที่มีฟิลด์ต่อไปนี้:


<th>ประเภท</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tr>
<td>Id</td>
<td>ตัวเลข 64 บิต</td>
<td>ID ผู้ใช้ของเพื่อน</td>
</tr>
<tr>
<td>ชื่อผู้ใช้</td>
<td>สตริง</td>
<td>ชื่อผู้ใช้ของเพื่อน</td>
</tr>
<tr>
<td>ชื่อที่แสดง</td>
<td>สตริง</td>
<td>คลาส <code>Class.Player.DisplayName|display name</code> ของเพื่อน</td>
</tr>
ชื่อ

ดูตัวอย่างโค้ดเพื่อวิธีที่ง่ายในการเลื่อนผ่านเพื่อนของผู้เล่นทั้งหมด

พารามิเตอร์

userId: number

รหัสผู้ใช้ของผู้เล่นที่ระบุ

ค่าเริ่มต้น: ""

ส่งค่ากลับ

ตัวอย่างโค้ด

This code sample loads the Player.UserId of the player whose username is provided at the top of the script by using Players:GetUserIdFromNameAsync(). Then, it gets a FriendPages object by calling Players:GetFriendsAsync() and iterates over each entry using the iterPageItems function. The username of each friend is stored in a table, then printed at the end.

Print Roblox Friends

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
-- First, get the user ID of the player
local userId = Players:GetUserIdFromNameAsync(USERNAME)
-- Then, get a FriendPages object for their friends
local friendPages = Players:GetFriendsAsync(userId)
-- Iterate over the items in the pages. For FriendPages, these
-- are tables of information about the friend, including Username.
-- Collect each username in a table
local usernames = {}
for item, _pageNo in iterPageItems(friendPages) do
table.insert(usernames, item.Username)
end
print("Friends of " .. USERNAME .. ": " .. table.concat(usernames, ", "))

GetHumanoidDescriptionFromOutfitId

ผลตอบแทน

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

พารามิเตอร์

outfitId: number

รหัสของชุดที่มีการค้นหาคำอธิบาย Humanoid

ค่าเริ่มต้น: ""

ส่งค่ากลับ

คำอธิบายมนุษย์ถูกเริ่มต้นด้วยข้อกำหนดสำหรับ outfitId ที่ส่งมา

ตัวอย่างโค้ด

Shows how to get the HumanoidDescription for bundle 799 (Fishman).

Get HumanoidDescription From Outfit ID

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:GetHumanoidDescriptionFromOutfitId(itemId)
end
return nil
end
local humanoidDescription = getHumanoidDescriptionBundle(799)
local humanoidModel = Players:CreateHumanoidModelFromDescription(humanoidDescription, Enum.HumanoidRigType.R15)
humanoidModel.Parent = Workspace

GetHumanoidDescriptionFromUserId

ผลตอบแทน

ส่งคืนคำอธิบายมนุษย์ที่ระบุถึงทุกอย่างที่ติดตั้งสำหรับอวตารของผู้ใช้ที่ระบุโดย userId ที่ส่งรวมถึงเครื่องชั่งและสีผิวด้วย

พารามิเตอร์

userId: number

ID สำหรับผู้ใช้ Roblox (UserId คือหมายเลขในโปรไฟล์ของผู้ใช้เช่น www.roblox.com/users/1/profile)

ค่าเริ่มต้น: ""

ส่งค่ากลับ

คำอธิบายมนุษย์ถูกเริ่มต้นด้วยข้อกำหนดของอวตารผู้ใช้ที่ส่งมา

ตัวอย่างโค้ด

This code sample shows how to use GetHumanoidDescriptionFromUserId() to create a Humanoid Model.

Get HumanoidDescription From User ID

game.Players:CreateHumanoidModelFromDescription(
game.Players:GetHumanoidDescriptionFromUserId(1),
Enum.HumanoidRigType.R15
).Parent =
game.Workspace

GetNameFromUserIdAsync

ผลตอบแทน

ฟังก์ชัน GetNameFromUserIdAsync Players จะส่งคำถามไปยังเว็บไซต์ Roblox โดยถามว่าชื่อผู้ใช้ของบัญชีมีอะไร UserId

วิธีนี้จะเกิดข้อผิดพลาดหากไม่มีบัญชีที่มี UserId ที่กำหนดหากคุณไม่แน่ใจว่าบัญชีดังกล่าวมีอยู่จริง แนะนำให้ห่อเรียกไปยังฟังก์ชันนี้ด้วย pcall()นอกจากนี้คุณสามารถแคชผลลัพธ์ด้วยตนเองเพื่อทำให้การโทรในอนาคตด้วย UserId เดียวกันรวดเร็วดูตัวอย่างโค้ดเพื่อเรียนรู้เพิ่มเติม

พารามิเตอร์

userId: number

The Player.UserId ของผู้เล่นที่ระบุไว้

ค่าเริ่มต้น: ""

ส่งค่ากลับ

ชื่อผู้ใช้ที่มีชื่อ Player.UserId ที่ระบุ

ตัวอย่างโค้ด

This code sample demonstrates using the Players:GetNameFromUserIdAsync() method to get a user's Player.Name from their Player.UserId.

Get Name from UserId

local Players = game:GetService("Players")
-- Example Data:
-- UserId: 118271 Name: "RobloxRulez"
-- UserId: 131963979 Name: "docsRule"
local nameOne = Players:GetNameFromUserIdAsync(118271)
local nameTwo = Players:GetNameFromUserIdAsync(131963979)
print(nameOne, nameTwo)
-- prints: "RobloxRulez docsRule"

This code sample demonstrates using the Players:GetNameFromUserIdAsync() method to get a user's Player.Name from their Player.UserId. Because GetNameFromUserIdAsync() yields, you can avoid calling it for the same Name using a table to store each UserId:Name pair found, called a cache. pcall() is used to catch the failure in case the Name doesn't exist.

Get Name from UserId using a cache

local Players = game:GetService("Players")
-- Create a table called 'cache' to store each 'Name' as they are found.
-- If we lookup a 'Name' using the same 'UserId', the 'Name' will come
-- from cache (fast) instead of GetNameFromUserIdAsync() (yields).
local cache = {}
function getNameFromUserId(userId)
-- First, check if the cache contains 'userId'
local nameFromCache = cache[userId]
if nameFromCache then
-- if a value was stored in the cache at key 'userId', then this 'nameFromCache'
-- is the correct Name and we can return it.
return nameFromCache
end
-- If here, 'userId' was not previously looked up and does not exist in the
-- cache. Now we need to use GetNameFromUserIdAsync() to look up the name
local name
local success, _ = pcall(function()
name = Players:GetNameFromUserIdAsync(userId)
end)
if success then
-- if 'success' is true, GetNameFromUserIdAsync() successfully found the
-- name. Store this name in the cache using 'userId' as the key so we
-- never have to look this name up in the future. Then return name.
cache[userId] = name
return name
end
-- If here, 'success' was false, meaning GetNameFromUserIdAsync()
-- was unable to find the 'name' for the 'userId' provided. Warn the user
-- this happened and then return nothing, or nil.
warn("Unable to find Name for UserId:", userId)
return nil
end
-- Example Data:
-- UserId: 118271 Name: "RobloxRulez"
-- UserId: 131963979 Name: "docsRule"
-- The first time a UserId is used, GetNameFromUserIdAsync() will be called
local nameOne = getNameFromUserId(118271)
local nameTwo = getNameFromUserId(131963979)
-- Because 118271 was previously used, get its Name from the cache
local nameOneQuick = getNameFromUserId(118271)
print(nameOne, nameTwo, nameOneQuick)
-- prints: "RobloxRulez docsRule RobloxRulez"

GetUserIdFromNameAsync

ผลตอบแทน

ฟังก์ชันนี้จะส่งคำถามไปยังเว็บไซต์ Roblox เพื่อถามว่า Player.UserId คืออะไรของบัญชีที่มีชื่อ Player ที่กำหนด

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

พารามิเตอร์

userName: string

ชื่อผู้ใช้ของผู้เล่นที่ระบุ

ค่าเริ่มต้น: ""

ส่งค่ากลับ

The Player.UserId ของผู้ใช้ที่ชื่อได้รับการระบุ

ตัวอย่างโค้ด

This code sample demonstrates using the Players:GetUserIdFromNameAsync() method to get a user's Player.UserId from their Player.Name.

Get UserId from Name

local Players = game:GetService("Players")
-- Example Data:
-- UserId: 118271 Name: "RobloxRulez"
-- UserId: 131963979 Name: "docsRule"
local userIdOne = Players:GetUserIdFromNameAsync("RobloxRulez")
local userIdTwo = Players:GetUserIdFromNameAsync("docsRule")
print(userIdOne, userIdTwo)
-- prints: "118271 131963979"

This code sample demonstrates using the Players:GetUserIdFromNameAsync() method to get a user's Player.UserId from their Player.Name. Because GetUserIdFromNameAsync() yields, you can avoid calling it for the same UserId using a table to store each Name:UserId pair found, called a cache. pcall() is used to catch the failure in case the UserId doesn't exist.

Get UserId from Name using a cache

local Players = game:GetService("Players")
-- Create a table called 'cache' to store each 'UserId' as they are found.
-- If we lookup a 'UserId' using the same 'Name', the 'UserId' will come
-- from cache (fast) instead of GetUserIdFromNameAsync() (yields).
local cache = {}
function getUserIdFromName(name)
-- First, check if the cache contains 'name'
local userIdFromCache = cache[name]
if userIdFromCache then
-- if a value was stored in the cache at key 'name', then this 'userIdFromCache'
-- is the correct UserId and we can return it.
return userIdFromCache
end
-- If here, 'name' was not previously looked up and does not exist in the
-- cache. Now we need to use GetUserIdFromNameAsync() to look up the userId
local userId
local success, _ = pcall(function()
userId = Players:GetUserIdFromNameAsync(name)
end)
if success then
-- if 'success' is true, GetUserIdFromNameAsync() successfully found the
-- userId. Store this userId in the cache using 'name' as the key so we
-- never have to look this userId up in the future. Then return userId.
cache[name] = userId
return userId
end
-- If here, 'success' was false, meaning GetUserIdFromNameAsync()
-- was unable to find the 'userId' for the 'name' provided. We can warn the
-- user this happened and then return nothing, or nil.
warn("Unable to find UserId for Name:", name)
return nil
end
-- Example Data:
-- UserId: 118271 Name: "RobloxRulez"
-- UserId: 131963979 Name: "docsRule"
-- The first time a Name is used, GetUserIdFromNameAsync() will be called
local userIdOne = getUserIdFromName("RobloxRulez")
local userIdTwo = getUserIdFromName("docsRule")
-- Because "RobloxRulez" was previously used, get its UserId from the cache
local userIdOneQuick = getUserIdFromName("RobloxRulez")
print(userIdOne, userIdTwo, userIdOneQuick)
-- prints: "118271 131963979 118271"

GetUserThumbnailAsync

ผลตอบแทน

ฟังก์ชันนี้คือการคืน URL เนื้อหาของภาพของอวตารของผู้เล่นที่ให้พวกเขา , ขนาดภาพที่ต้องการเป็น enum, และประเภทที่ต้องการเป็น enumนอกจากนี้ยังส่งคืนบูลีนที่อธิบายว่าภาพพร้อมใช้งานหรือไม่

บ่อยที่สุดวิธีนี้จะใช้กับ ImageLabel.Image หรือ Decal.Texture เพื่อแสดงภาพตัวละครผู้ใช้ในประสบการณ์

พารามิเตอร์

userId: number

The Player.UserId ของผู้เล่นที่ระบุไว้

ค่าเริ่มต้น: ""
thumbnailType: Enum.ThumbnailType

A Enum.ThumbnailType ภาพขนาดย่อ

ค่าเริ่มต้น: ""
thumbnailSize: Enum.ThumbnailSize

A Enum.ThumbnailSize ภาพขนาดย่อ

ค่าเริ่มต้น: ""

ส่งค่ากลับ

tuple ที่มี URL เนื้อหาของภาพขนาดย่อของผู้ใช้ตามพารามิเตอร์ที่ระบุและ bool ที่อธิบายว่าภาพพร้อมใช้งานหรือไม่

ตัวอย่างโค้ด

This code sample displays the current player's thumbnail in a parent ImageLabel by using Players:GetUserThumbnailAsync() and setting the Image() property as well as its Size().

Display Player Thumbnail

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local PLACEHOLDER_IMAGE = "rbxassetid://0" -- replace with placeholder image
-- fetch the thumbnail
local userId = player.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
-- set the ImageLabel's content to the user thumbnail
local imageLabel = script.Parent
imageLabel.Image = (isReady and content) or PLACEHOLDER_IMAGE
imageLabel.Size = UDim2.new(0, 420, 0, 420)

UnbanAsync

()
ผลตอบแทน

ยกเลิกการแบนผู้เล่นที่ถูกแบนจาก Players:BanAsync() หรือ ข้อจํากัดการใช้งานของผู้ใช้เปิด API คลาวด์วิธีนี้เปิดใช้งานและปิดใช้งานโดยคุณสมบัติ Players.BanningEnabled ซึ่งคุณสามารถสลับได้ใน Studio

เช่น Players:BanAsync() วิธีนี้จะใช้สารบัญ config ที่จะช่วยให้คุณสามารถยกเลิกการแบนผู้ใช้จำนวนมากได้การกำหนดค่านี้จะกำหนดผู้ใช้ที่ไม่ถูกแบนและขอบเขตที่พวกเขาไม่ถูกแบนจาก

การยกเลิกการแบนจะมีผลเฉพาะกับการแบนที่มีขอบเขตเดียวกัน ApplyToUniverseตัวอย่างเช่น การยกเลิกการแบนด้วย ApplyToUniverse ตั้งค่าเป็น true จะไม่ทำให้การแบนก่อนหน้านี้เป็นโมฆะด้วย ApplyToUniverse ตั้งค่าเป็น falseในคำอื่น ๆ การยกเลิกระดับจักรวาลจะไม่ทำให้การห้ามระดับสถานที่เป็นโมฆะสิ่งตรงกันข้ามก็เป็นจริงเช่นกัน

วิธีนี้เรียกการโทร HTTP ไปยังบริการด้านหลังที่ถูกจำกัดและอาจล้มเหลวหากคุณเรียก API นี้ด้วยหลายรหัสผู้ใช้ วิธีนี้จะพยายามทำการโทร HTTP สำหรับแต่ละรหัสผู้ใช้จากนั้นจะรวบรวมข้อความข้อผิดพลาดและรวมเข้ากับพวกเขาเป็นรายการแยกกับจุลภาคตัวอย่างเช่นหากวิธีนี้ถูกเรียกใช้สำหรับห้า : และคำขอสำหรับผู้ใช้ 2 และ 4 ล้มเหลวแล้วข้อความข้อผิดพลาดต่อไปนี้จะปรากฏขึ้น: ข้อความจะรวมถึง เสมอหากเป็นข้อผิดพลาด HTTPเป็นพฤติกรรมที่ไม่ได้กำหนดไว้หากคุณส่งทั้ง UserIds ที่ถูกต้องและไม่ถูกต้อง เช่นa UserId ที่ไม่ใช่ตัวเลขบวกเนื่องจากคำขอเครือข่ายบางอย่างอาจประสบความสำเร็จก่อนที่จะมีการตรวจสอบข้อมูลทั้งหมด

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

API นี้ใช้ ข้อจํากัดการใช้งานของผู้ใช้เปิด API เมฆ คุณจะสามารถใช้ API เหล่านี้เพื่อจัดการการแบนของคุณในแอปพลิเคชันบุคคลที่สามได้

พารามิเตอร์

config: Dictionary

<th>ประเภท</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>รหัสผู้ใช้</code></td>
<td>รายการ</td>
<td>UserIDs ที่จะถูกบังคับให้เข้าสู่ประสบการณ์ (s) ขนาดสูงสุดคือ <code>50</code></td>
</tr>
<tr>
<td><code>ใช้กับจักรวาล</code></td>
<td>เป็นไปได้</td>
<td>เผยแพร่การยกเลิกการแบนไปยังทุกสถานที่ภายในจักรวาลนี้</td>
</tr>
</tbody>
ชื่อ
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

The following un-bans a user, as well as another unrelated account with UserId 789.

Unbanning Users

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

อีเวนต์นี้จะเกิดขึ้นเมื่อผู้เล่นเข้าสู่เกมใช้เพื่อยิงเหตุการณ์เมื่อผู้เล่นเข้าร่วมเกม เช่น การโหลดข้อมูลที่บันทึกของผู้เล่น GlobalDataStore

สิ่งนี้สามารถใช้ร่วมกับอีเวนต์ Players.PlayerRemoving อินสแตนซ์หากคุณต้องการพิมพ์ข้อความทุกครั้งที่ผู้เล่นใหม่เข้าร่วมหรือออกจากเกม:


local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
end)
Players.PlayerRemoving:Connect(function(player)
print(player.Name .. " left the game!")
end)

หากคุณต้องการติดตามเมื่อตัวละครของผู้เล่นถูกเพิ่มหรือลบออกจากเกม เช่น เมื่อผู้เล่นเกิดใหม่หรือตาย คุณสามารถใช้ฟังก์ชัน Player.CharacterAdded และ Player.CharacterRemoving ได้

โปรดทราบว่าอีเวนต์นี้ไม่ทํางานตามที่คาดไว้ในโหมด เล่น เนื่องจากผู้เล่นถูกสร้างก่อนที่จะรันสคริปต์ที่เชื่อมต่อกับ PlayerAddedเพื่อจัดการกรณีนี้และกรณีที่สคริปต์ถูกเพิ่มลงในเกมหลังจากที่ผู้เล่นเข้ามา สร้างฟังก์ชัน onPlayerAdded() ที่คุณสามารถโทรได้เพื่อจัดการการเข้าของผู้เล่น

พารามิเตอร์

player: Player

ตัวอย่างของผู้เล่นที่เข้าร่วมเกม


ตัวอย่างโค้ด

This example will print "A player has entered: " followed by the name of the player that enters/joins a game every time a player joins.

Players.PlayerAdded

local Players = game:GetService("Players")
local function onPlayerAdded(player)
print("A player has entered: " .. player.Name)
end
Players.PlayerAdded:Connect(onPlayerAdded)

PlayerMembershipChanged

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

เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับและรวมเบี้ยพรีเมียมในประสบการณ์ของคุณและสร้างรายได้ด้วยระบบการชำระเงินแบบมุ่งเน้นการมีส่วนร่วม ดู การชำระเงินแบบมุ่งเน้นการมีส่วนร่วม

ดูเพิ่ม:

พารามิเตอร์

player: Player

ตัวอย่างโค้ด

The function in the code sample runs after the game server confirms a player's membership has changed. It demonstrates how you can grant players access to Premium benefits (or revoke them) when their membership status changes.

Handling Premium Membership Changes

local Players = game:GetService("Players")
local function grantPremiumBenefits(player)
-- Grant the player access to Premium-only areas, items, or anything you can imagine!
print("Giving", player, "premium benefits!")
end
local function playerAdded(player)
if player.MembershipType == Enum.MembershipType.Premium then
grantPremiumBenefits(player)
end
end
local function playerMembershipChanged(player)
print("Received event PlayerMembershipChanged. New membership = " .. tostring(player.MembershipType))
if player.MembershipType == Enum.MembershipType.Premium then
grantPremiumBenefits(player)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerMembershipChanged:Connect(playerMembershipChanged)

PlayerRemoving

อีเวนต์การลบผู้เล่นจะเกิดขึ้นก่อนที่ Player จะออกจากเกมอีเวนต์นี้จะเกิดขึ้นก่อน ChildRemoved ทำบน Players และมีพฤติกรรมคล้ายกับ Instance.DescendantRemoving เล็กน้อยเนื่องจากมันจะยิงก่อนการลบจริงของ Player จึงเป็นประโยชน์สำหรับการจัดเก็บข้อมูลผู้เล่นโดยใช้ GlobalDataStore

สิ่งนี้สามารถใช้ร่วมกับอีเวนต์ Player.PlayerAdded ซึ่งจะเปิดเมื่อผู้เล่นเข้าร่วมเกมตัวอย่างเช่นเพื่อพิมพ์ข้อความทุกครั้งที่ผู้เล่นใหม่เข้าร่วมหรือออกจากเกม:


local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
end)
Players.PlayerRemoving:Connect(function(player)
print(player.Name .. " left the game!")
end)

หากคุณต้องการติดตามเมื่อตัวละครของผู้เล่นถูกเพิ่มหรือลบออกจากเกม เช่น เมื่อผู้เล่นเกิดใหม่หรือตาย คุณสามารถใช้ฟังก์ชัน Player.CharacterAdded และ Player.CharacterRemoving ได้

พารามิเตอร์

player: Player

ตัวอย่างของผู้เล่นที่กำลังออกจากเกม


ตัวอย่างโค้ด

This code will print "A player has left: ", followed by the player's name, every time a player leaves:

Players.PlayerRemoving

local Players = game:GetService("Players")
local function onPlayerRemoving(player)
print("A player has left: " .. player.Name)
end
Players.PlayerRemoving:Connect(onPlayerRemoving)

UserSubscriptionStatusChanged

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

โปรดทราบว่าเฉพาะสคริปต์เซิร์ฟเวอร์เท่านั้นที่ได้รับเหตุการณ์นี้

พารามิเตอร์

user: Player

ผู้ใช้ที่สถานะการสมัครรับข้อมูลมีการเปลี่ยนแปลง

subscriptionId: string

รหัสสมัครรับข้อมูลที่มีการเปลี่ยนแปลงสถานะ