Players
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
คุณสมบัติ
เปิดใช้งานหรือปิดใช้งานวิธีการสาม Players วิธี ( BanAsync() , UnbanAsync() และ GetBanHistoryAsync() ) ที่ประกอบขึ้นเป็น API การแบนคุณสมบัตินี้ไม่สามารถสคริปต์ได้และสามารถแก้ไขได้เฉพาะใน Studio
บ่งบอกว่าการแชทฟองอากาศถูกเปิดใช้งานหรือไม่ ตั้งค่าด้วยวิธี Players:SetChatStyle()
บ่งบอกว่า characters จะเกิดใหม่โดยอัตโนมัติหรือไม่
บ่งบอกว่าจะมีการเปิดใช้งานการแชทแบบคลาสสิกหรือไม่; ตั้งโดยวิธี Players:SetChatStyle()
The Player ที่ LocalScript กําลังทํางานอยู่
จํานวนผู้เล่นสูงสุดที่สามารถอยู่ในเซิร์ฟเวอร์ได้
จํานวนผู้เล่นที่ต้องการสําหรับเซิร์ฟเวอร์
ควบคุมจำนวนเวลาที่ใช้สำหรับตัวละครผู้เล่นที่จะเกิดใหม่
วิธีการ
ทำให้ผู้เล่นท้องถิ่นแชทข้อความที่กำหนดไว้
คืนค่า Player ซึ่ง Player.Character อินสแตนซ์nil หากไม่พบได้
คืนตารางของวัตถุทั้งหมดที่เชื่อมต่ออยู่ในปัจจุบัน Player อยู่
ตั้งค่าว่า BubbleChat และ ClassicChat จะถูกใช้หรือไม่ และบอก TeamChat และ Chat สิ่งที่ต้องทำ
ทำให้ผู้เล่นท้องถิ่นแชทข้อความที่กำหนด ซึ่งจะมองเห็นได้เฉพาะผู้ใช้ในทีมเดียวกันเท่านั้น
ห้ามผู้ใช้จากประสบการณ์ของคุณด้วยตัวเลือกในการระบุระยะเวลา เหตุผล ว่าการแบนมีผลกับจักรวาลทั้งหมดหรือเพียงแค่สถานที่ปัจจุบันเท่านั้น และอื่นๆวิธีนี้เปิดใช้งานและปิดใช้งานโดยคุณสมบัติ Players.BanningEnabled ซึ่งคุณสามารถสลับได้ใน Studio
- CreateHumanoidModelFromDescription(description : HumanoidDescription,rigType : Enum.HumanoidRigType,assetTypeVerification : Enum.AssetTypeVerification):Model
ส่งคืนโมเดลตัวละครที่ติดตั้งทุกอย่างตามที่ระบุไว้ใน 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 พิมพ์รวมถึงบูลีนที่อธิบายถึงว่าภาพพร้อมใช้งานหรือไม่
ยกเลิกการแบนผู้เล่นที่ถูกแบนจาก Players:BanAsync() หรือ Cloud APIPlayers.BanningEnabled ซึ่งคุณสามารถสลับได้ใน Studio
เหตุการณ์
เกิดไฟไหม้เมื่อผู้เล่นเข้าสู่เกม
ไฟเมื่อเซิร์ฟเวอร์เกมรู้ว่าสมาชิกของผู้เล่นเปลี่ยนแปลงแล้ว
เกิดไฟไหม้เมื่อผู้เล่นกำลังจะออกจากเกม
เกิดไฟไหม้เมื่อเซิร์ฟเวอร์เกมรู้ว่าสถานะของผู้ใช้สำหรับสมัครสมาชิกบางอย่างได้เปลี่ยนแปลง
คุณสมบัติ
BanningEnabled
BubbleChat
CharacterAutoLoads
ตัวอย่างโค้ด
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
LocalPlayer
MaxPlayers
PreferredPlayers
RespawnTime
UseStrafingAnimations
วิธีการ
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
ส่งค่ากลับ
ตัวอย่างโค้ด
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)
SetChatStyle
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
-- แถบคําสั่ง
game.Players:SetChatStyle(Enum.ChatStyle.Classic) -- ตั้งสไตล์การแชทเป็นแบบคลาสสิก
-- สคริปท์ท้องถิ่น
local Players = game:GetService("Players")
Players:SetChatStyle(Enum.ChatStyle.Classic) -- Errors
TeamChat
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
-- แถบคําสั่ง
game.Players:TeamChat("Hello World") -- ส่งข้อความ "สวัสดีชาวโลก" ไปยังผู้เล่นทุกคนในทีมผู้เล่นท้องถิ่น
-- สคริปท์ท้องถิ่น
local Players = game:GetService("Players")
Players:TeamChat("Hello World") -- Errors
BanAsync
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
game.Players:CreateHumanoidModelFromDescription(Instance.new("HumanoidDescription"), Enum.HumanoidRigType.R15).Parent =
game.Workspace
CreateHumanoidModelFromUserId
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
game.Players:CreateHumanoidModelFromUserId(1).Parent = game.Workspace
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 = "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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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 connections
local friendPages = Players:GetFriendsAsync(userId)
-- Iterate over the items in the pages. For FriendPages, these
-- are tables of information about the connection, including Username.
-- Collect each username in a table
local usernames = {}
for item, _pageNo in iterPageItems(friendPages) do
table.insert(usernames, item.Username)
end
print("Connections of " .. USERNAME .. ": " .. table.concat(usernames, ", "))
GetHumanoidDescriptionFromOutfitId
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
game.Players:CreateHumanoidModelFromDescription(
game.Players:GetHumanoidDescriptionFromUserId(1),
Enum.HumanoidRigType.R15
).Parent =
game.Workspace
GetNameFromUserIdAsync
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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"
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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"
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
พารามิเตอร์
ตัวอย่างโค้ด
local Players = game:GetService("Players")
local function onPlayerAdded(player)
print("A player has entered: " .. player.Name)
end
Players.PlayerAdded:Connect(onPlayerAdded)
PlayerMembershipChanged
พารามิเตอร์
ตัวอย่างโค้ด
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
พารามิเตอร์
ตัวอย่างโค้ด
local Players = game:GetService("Players")
local function onPlayerRemoving(player)
print("A player has left: " .. player.Name)
end
Players.PlayerRemoving:Connect(onPlayerRemoving)