Players
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ข้อมูลสรุป
คุณสมบัติ
วิธีการ
เหตุการณ์
PlayerAdded(player: Player):RBXScriptSignal |
PlayerMembershipChanged(player: Player):RBXScriptSignal |
PlayerRemoving(player: Player,reason: Enum.PlayerExitReason):RBXScriptSignal |
UserSubscriptionStatusChanged(user: Player,subscriptionId: string):RBXScriptSignal |
เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
Character Auto Loads
ตัวอย่างโค้ด
ตัวจับเวลาเกิดใหม่ของผู้เล่น
local Players = game:GetService("Players")
-- ตั้งค่า CharacterAutoLoads เป็น false
Players.CharacterAutoLoads = false
-- ลบตัวละครของผู้เล่นออกจาก workspace เมื่อเสียชีวิต
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)
endlocal Player
Num Players
num Players
วิธีการ
Ban Async
Players: Ban Async(config:BanConfigType):()
พารามิเตอร์
config:BanConfigType |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การแบนผู้ใช้
local Players = game:GetService("Players")
local function shouldBeBanned(player: Player): boolean
-- แทนที่ด้วยตรรกะการควบคุมของคุณเอง
return false
end
Players.PlayerAdded:Connect(function(player)
if shouldBeBanned(player) then
local banHistoryPages = Players:GetBanHistoryAsync(player.UserId)
print(banHistoryPages:GetCurrentPage())
local duration = 86400 -- 24 ชั่วโมงในวินาที แนะนำ: ใช้ตรรกะของคุณเองตามประวัติการแบนของผู้ใช้
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)
end
end)Chat
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
Players:Chat
-- Command bar
game:GetService("Players"):Chat("สวัสดี, โลก!") --ส่งผลให้ 'สวัสดี, โลก!' ปรากฏในบันทึกแชทใต้ชื่อผู้เล่นของคุณ。
-- Script
local Players = game:GetService("Players")
Players:Chat("สวัสดี, โลก!") --เกิดข้อผิดพลาดCreate Humanoid Model From Description
Create Humanoid Model From Description Async
Players: Create Humanoid Model From Description Async(
description:HumanoidDescription, rig Type:Enum.HumanoidRigType, asset Type Verification:Enum.AssetTypeVerification
พารามิเตอร์
| ค่าเริ่มต้น: Default |
ส่งค่ากลับ
ตัวอย่างโค้ด
สร้างโมเดลมนุษย์จากคำอธิบาย
local Players = game:GetService("Players")
-- สร้างโมเดลมนุษย์จากคำอธิบาย
Players:CreateHumanoidModelFromDescriptionAsync(Instance.new("HumanoidDescription"), Enum.HumanoidRigType.R15).Parent =
game.WorkspaceCreate Humanoid Model From User Id
Create Humanoid Model From User Id Async
Get Ban History Async
พารามิเตอร์
ส่งค่ากลับ
Get Character Appearance Async
Get Character Appearance Info Async
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ข้อมูลลักษณะตัวละคร
local Players = game:GetService("Players")
-- รหัสผู้ใช้ที่ต้องการค้นหา (1 คือบัญชี Roblox)
local USER_ID = 1
-- การเรียกนี้จะไปยังเว็บไซต์ Roblox ดังนั้นอาจล้มเหลว
local success, appearanceInfo = pcall(function()
return Players:GetCharacterAppearanceInfoAsync(USER_ID)
end)
if not success then
warn("ไม่สามารถดึงข้อมูลลักษณะได้:", appearanceInfo)
return
end
print("ประเภทอวตาร:", appearanceInfo.playerAvatarType) --> R15 หรือ R6
print("สัดส่วนความสูง:", appearanceInfo.scales.height)
-- แสดงทรัพย์สินทุกชิ้นที่อวตารสวมใส่
for _, asset in appearanceInfo.assets do
print(`{asset.assetType.name}: {asset.name} ({asset.id})`)
endGet Friends Async
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
พิมพ์เพื่อนใน Roblox
local Players = game:GetService("Players")
local USERNAME = "Cozecant"
-- ก่อนอื่น, รับ ID ของผู้เล่น
local userId = Players:GetUserIdFromNameAsync(USERNAME)
-- จากนั้น, รับวัตถุ FriendPages สำหรับเพื่อนของพวกเขา
local friendPages = Players:GetFriendsAsync(userId)
-- เดินผ่านหน้าต่าง, เก็บชื่อผู้ใช้ของเพื่อนทุกคน. แต่ละรายการในหน้า
-- เป็นตารางข้อมูลเกี่ยวกับเพื่อน, รวมถึงชื่อผู้ใช้.
local usernames = {}
while true do
for _, friend in ipairs(friendPages:GetCurrentPage()) do
table.insert(usernames, friend.Username)
end
if friendPages.IsFinished then
break
end
friendPages:AdvanceToNextPageAsync()
end
print("เพื่อนของ " .. USERNAME .. ": " .. table.concat(usernames, ", "))Get Humanoid Description From Outfit Id
Get Humanoid Description From Outfit Id Async
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ดึง HumanoidDescription จาก Outfit ID
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local AssetService = game:GetService("AssetService")
local function getOutfitId(bundleId)
if bundleId <= 0 then
return
end
local info = 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 = WorkspaceGet Humanoid Description From User Id
Get Humanoid Description From User Id Async
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ดึง HumanoidDescription จาก User ID
local Players = game:GetService("Players")
-- สร้างโมเดลมนุษย์จากคำอธิบาย
Players:CreateHumanoidModelFromDescriptionAsync(
Players:GetHumanoidDescriptionFromUserIdAsync(1),
Enum.HumanoidRigType.R15
).Parent =
game.WorkspaceGet Name From User Id Async
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ดึงชื่อจาก UserId
local Players = game:GetService("Players")
-- ข้อมูลตัวอย่าง:
-- UserId: 118271 ชื่อ: "RobloxRulez"
-- UserId: 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' แต่ละตัวเมื่อพบ.
-- หากเราค้นหา 'Name' โดยใช้ 'UserId' เดิม, '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' ที่ให้มาได้ แจ้งเตือนผู้ใช้
-- ว่าสิ่งนี้เกิดขึ้นและจากนั้นส่งคืนไม่มีอะไร, หรือ nil.
warn("ไม่สามารถหาชื่อสำหรับ UserId:", userId)
return nil
end
-- ข้อมูลตัวอย่าง:
-- UserId: 118271 Name: "RobloxRulez"
-- UserId: 131963979 Name: "docsRule"
-- ครั้งแรกที่ใช้ UserId, GetNameFromUserIdAsync() จะถูกเรียก
local nameOne = getNameFromUserId(118271)
local nameTwo = getNameFromUserId(131963979)
-- เนื่องจาก 118271 ถูกใช้ก่อนหน้านี้, ดึงชื่อจากแคช
local nameOneQuick = getNameFromUserId(118271)
print(nameOne, nameTwo, nameOneQuick)
-- พิมพ์: "RobloxRulez docsRule RobloxRulez"Get Player By User Id
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
Players:GetPlayerByUserId
local Players = game:GetService("Players")
local player = Players:GetPlayerByUserId(1)
if player then
print("ผู้เล่นที่มี userId 1 อยู่ในเซิร์ฟเวอร์นี้! ชื่อของพวกเขาคือ: " .. player.Name)
else
print("ผู้เล่นที่มี userId 1 ไม่อยู่ในเซิร์ฟเวอร์นี้!")
endGet Player From Character
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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.Name} ({player.UserId}) อยู่ในเกม`)
else
print(`ผู้เล่น {PLAYER_NAME} ไม่อยู่ในเกม!`)
endGet Players
ส่งค่ากลับ
{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)get Players
Get Profile Configuration From User Id Async
Get User Id From Name Async
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
รับ UserId จากชื่อ
local Players = game:GetService("Players")
-- ข้อมูลตัวอย่าง:
-- UserId: 118271 ชื่อ: "RobloxRulez"
-- UserId: 131963979 ชื่อ: "docsRule"
local userIdOne = Players:GetUserIdFromNameAsync("RobloxRulez")
local userIdTwo = Players:GetUserIdFromNameAsync("docsRule")
print(userIdOne, userIdTwo)
-- แสดงผล: "118271 131963979"ดึง UserId จากชื่อโดยใช้แคช
local Players = game:GetService("Players")
-- สร้างตารางชื่อ 'cache' เพื่อเก็บ 'UserId' แต่ละตัวเมื่อพบ.
-- หากเราค้นหา 'UserId' โดยใช้ 'Name' เดิม, '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' เป็นจริง, GetUserIdFromNameAsync() พบ
-- userId สำเร็จแล้ว เก็บ userId นี้ในแคชโดยใช้ 'name' เป็นคีย์
-- เพื่อที่เราจะไม่ต้องค้นหา userId นี้ในอนาคต จากนั้นส่งคืน userId.
cache[name] = userId
return userId
end
-- หากมาที่นี่, 'success' เป็นเท็จ, หมายความว่า GetUserIdFromNameAsync()
-- ไม่สามารถหาค่า 'userId' สำหรับ 'name' ที่ให้มาได้ เราสามารถเตือนผู้ใช้
-- ว่าสิ่งนี้เกิดขึ้นและจากนั้นส่งคืนไม่มีค่า หรือ nil.
warn("ไม่สามารถหาค่า UserId สำหรับชื่อ:", name)
return nil
end
-- ข้อมูลตัวอย่าง:
-- UserId: 118271 ชื่อ: "RobloxRulez"
-- UserId: 131963979 ชื่อ: "docsRule"
-- ครั้งแรกที่ใช้ชื่อ, GetUserIdFromNameAsync() จะถูกเรียก
local userIdOne = getUserIdFromName("RobloxRulez")
local userIdTwo = getUserIdFromName("docsRule")
-- เนื่องจาก "RobloxRulez" ถูกใช้ก่อนหน้านี้, ดึง UserId ของมันจากแคช
local userIdOneQuick = getUserIdFromName("RobloxRulez")
print(userIdOne, userIdTwo, userIdOneQuick)
-- พิมพ์: "118271 131963979 118271"Get User Thumbnail Async
Players: Get User Thumbnail Async(
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
แสดงภาพโปรไฟล์ผู้เล่น
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.fromOffset(420, 420)player From Character
players
Set Chat Style
พารามิเตอร์
| ค่าเริ่มต้น: Classic |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การตั้งค่ารูปแบบการแชทของผู้เล่น
-- แถบคำสั่ง
game:GetService("Players"):SetChatStyle(Enum.ChatStyle.Classic) -- ตั้งค่ารูปแบบการแชทเป็น Classic
-- LocalScript
local Players = game:GetService("Players")
Players:SetChatStyle(Enum.ChatStyle.Classic) -- เกิดข้อผิดพลาดTeam Chat
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ส่งข้อความทีม
-- แถบคำสั่ง
game:GetService("Players"):TeamChat("สวัสดีชาวโลก") -- ส่งข้อความ "สวัสดีชาวโลก" ไปยังผู้เล่นทั้งหมดในทีมของผู้เล่นท้องถิ่น
-- LocalScript
local Players = game:GetService("Players")
Players:TeamChat("สวัสดีชาวโลก") -- เกิดข้อผิดพลาดUnban Async
Players: Unban Async(config:UnbanConfigType):()
พารามิเตอร์
config:UnbanConfigType |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การปลดแบนผู้ใช้
local Players = game:GetService("Players")
local function shouldBeUnbanned(player: Player): boolean
-- แทนที่ด้วยตรรกะของคุณเอง
return true
end
Players.PlayerAdded:Connect(function(player)
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
end)เหตุการณ์
Player Added
พารามิเตอร์
ตัวอย่างโค้ด
Players.PlayerAdded
local Players = game:GetService("Players")
local function onPlayerAdded(player)
print("ผู้เล่นได้เข้ามา: " .. player.Name)
end
Players.PlayerAdded:Connect(onPlayerAdded)Player Membership Changed
พารามิเตอร์
ตัวอย่างโค้ด
การจัดการการเปลี่ยนแปลงสมาชิกพรีเมียม
local Players = game:GetService("Players")
local function grantPremiumBenefits(player)
-- ให้ผู้เล่นเข้าถึงพื้นที่, ไอเท็ม, หรือสิ่งใดก็ตามที่คุณสามารถจินตนาการได้เฉพาะพรีเมียม!
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)Player Removing
พารามิเตอร์
ตัวอย่างโค้ด
Players.PlayerRemoving
local Players = game:GetService("Players")
local function onPlayerRemoving(player)
print("ผู้เล่นได้ออกจากเกม: " .. player.Name)
end
Players.PlayerRemoving:Connect(onPlayerRemoving)User Subscription Status Changed