Engine Class
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
คุณสมบัติ
CharacterAutoLoads
ตัวอย่างโค้ด
ตัวจับเวลาการเกิดใหม่ของผู้เล่น
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)
-- รีสปawns ผู้เล่นที่ตายทั้งหมดทุกๆ 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 วินาทีก่อนตรวจสอบการรีสปawns ครั้งถัดไป
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 ชั่วโมงในวินาที แนะนำ: ใช้ตรรกะของคุณเองตามประวัติการแบนของผู้ใช้
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 = "หมวก",
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 Boy",
},
{
id = 376531012,
assetType = {
name = "แขนขวา",
id = 28,
},
name = "แขนขวาของ ROBLOX Boy",
},
{
id = 376531300,
assetType = {
name = "ขาซ้าย",
id = 30,
},
name = "ขาซ้ายของ ROBLOX Boy",
},
{
id = 376531703,
assetType = {
name = "ขาขวา",
id = 31,
},
name = "ขาขวาของ ROBLOX Boy",
},
{
id = 376532000,
assetType = {
name = "ลำตัว",
id = 27,
},
name = "ลำตัวของ ROBLOX Boy",
},
},
}
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
รับ HumanoidDescription จาก 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:GetHumanoidDescriptionFromOutfitIdAsync(itemId)
end
return nil
end
local humanoidDescription = getHumanoidDescriptionBundle(799)
local humanoidModel = Players:CreateHumanoidModelFromDescriptionAsync(humanoidDescription, Enum.HumanoidRigType.R15)
humanoidModel.Parent = WorkspaceGetHumanoidDescriptionFromUserId
GetHumanoidDescriptionFromUserIdAsync
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ดึง HumanoidDescription จาก User ID
game.Players:CreateHumanoidModelFromDescriptionAsync(
game.Players:GetHumanoidDescriptionFromUserIdAsync(1),
Enum.HumanoidRigType.R15
).Parent =
game.WorkspaceGetNameFromUserIdAsync
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ดึงชื่อจาก 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"GetPlayerByUserId
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ผู้เล่น:GetPlayerByUserId
local Players = game:GetService("Players")
local player = Players:GetPlayerByUserId(1)
if player then
print("ผู้เล่นที่มี userId 1 อยู่ในเซิร์ฟเวอร์นี้! ชื่อของพวกเขาคือ: " .. player.Name)
else
print("ผู้เล่นที่มี userId 1 ไม่มีในเซิร์ฟเวอร์นี้!")
endGetPlayerFromCharacter
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ผู้เล่น: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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
รับ 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' สำหรับชื่อที่ให้ไว้ เราสามารถเตือนผู้ใช้
-- ว่านี่เกิดขึ้นและจากนั้นไม่ส่งกลับอะไร, หรือ 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" |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การตั้งค่าสไตล์การแชทของผู้เล่น
-- Command bar
game.Players:SetChatStyle(Enum.ChatStyle.Classic) -- ตั้งค่าสไตล์การแชทเป็น Classic
-- LocalScript
local Players = game:GetService("Players")
Players:SetChatStyle(Enum.ChatStyle.Classic) -- เกิดข้อผิดพลาดTeamChat
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ส่งข้อความทีม
-- Command bar
game.Players:TeamChat("Hello World") -- ส่งข้อความ "Hello World" ไปยังผู้เล่นทั้งหมดในทีมของผู้เล่นท้องถิ่น
-- LocalScript
local Players = game:GetService("Players")
Players:TeamChat("Hello World") -- เกิดข้อผิดพลาด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("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
พารามิเตอร์
ตัวอย่างโค้ด
ผู้เล่น.PlayerRemoving
local Players = game:GetService("Players")
local function onPlayerRemoving(player)
print("ผู้เล่นออกจากเกม: " .. player.Name)
end
Players.PlayerRemoving:Connect(onPlayerRemoving)UserSubscriptionStatusChanged