Player

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

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

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

คุณสมบัติ Instance.Name สะท้อนชื่อผู้ใช้ของผู้เล่นเมื่อบันทึกข้อมูลเกี่ยวกับผู้เล่นคุณควรใช้ Player.UserId เนื่องจากเป็นไปได้ว่าผู้เล่นสามารถเปลี่ยนชื่อผู้ใช้ได้

มีหลายวิธีที่คล้ายกันในบริการ สำหรับการทำงานกับวัตถุผู้เล่น ใช้วิธีเหล่านี้เหนือวิธีการที่เฉพาะเจาะจงของพวกเขา:

  • คุณสามารถรับตารางวัตถุผู้เล่นปัจจุบันโดยใช้ Players:GetPlayers() ; อีกครั้งใช้สิ่งนี้แทน Instance:GetChildren()
  • เพื่อตรวจจับการเพิ่มของวัตถุผู้เล่น แนะนำให้ใช้เหตุการณ์ Players.PlayerAdded (แทน Instance.ChildAdded บนบริการ Players)
  • เช่นเดียวกันคุณสามารถตรวจพบการลบวัตถุผู้เล่นโดยใช้ ซึ่งจะยิงเพียง ก่อนที่ผู้เล่นจะถูกลบ (แทนที่จะยิงหลังจากที่ ซึ่งจะยิงหลังจากนั้น)เป็นสิ่งสำคัญหากคุณกำลังบันทึกข้อมูลเกี่ยวกับผู้เล่นที่อาจถูกลบหรือทำความสะอาดในระหว่างการลบ

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

This code sample demonstrates the creation of leaderboard stat values in Roblox's default player list UI. It creates a "Score" leaderstat that starts at 0.

Leaderstats

local Players = game:GetService("Players")
local function onPlayerAdded(player)
-- Create a container for leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
-- Create one leaderstat value
local vScore = Instance.new("IntValue")
vScore.Name = "Score"
vScore.Value = 0
vScore.Parent = leaderstats
-- Add to player (displaying it)
leaderstats.Parent = player
end
Players.PlayerAdded:Connect(onPlayerAdded)

สรุป

คุณสมบัติ

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

    อธิบายอายุบัญชีผู้เล่นในวันที่

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

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

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

    ระยะสูงสุดที่กล้องของผู้เล่นสามารถซูมออกได้

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

    ระยะขั้นต่ำที่กล้องของผู้เล่นสามารถซูมได้

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

    เปลี่ยนโหมดกล้องเป็นคนแรกหรือคนที่สาม

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

    กำหนดว่ารูปลักษณ์ของตัวละครจะถูกโหลดเมื่อผู้เล่นเกิดหรือไม่ หากเป็นไปไม่ได้ ผู้เล่นจะเกิดขึ้นด้วยรูปลักษณ์เริ่มต้น

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

    A Model ควบคุมโดยผู้เล่นที่มี Humanoid ส่วนตัว สคริปต์ และวัตถุอื่นๆ

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

    กำหนดรหัสผู้ใช้ของบัญชีที่มีตัวละครปรากฏใช้สำหรับผู้เล่นของผู้เล่น character

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

    กำหนดวิธีที่กล้องเริ่มต้นจัดการวัตถุระหว่างกล้องและผู้เล่น

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

    กำหนดโหมดการเคลื่อนไหวของกล้องผู้เล่นเมื่อใช้เวอร์ชันหน้าต่างของ Roblox

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

    กำหนดโหมดการเคลื่อนที่ตัวละครของผู้เล่นเมื่อใช้เวอร์ชันหน้าต่างของ Roblox

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

    กำหนดว่าผู้เล่นสามารถสลับการล็อคเมาส์ได้หรือไม่

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

    กำหนดโหมดการเคลื่อนไหวของกล้องของผู้เล่นเมื่อใช้อุปกรณ์สัมผัส

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

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

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

    ชื่อที่แสดงของ UserId ที่เกี่ยวข้องกับผู้เล่น

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

    อธิบายรหัสผู้ใช้ของผู้เล่นที่ถูกติดตามเข้าสู่เกมโดยผู้เล่น

  • การรักษาความปลอดภัยที่เข้าถึงไม่ได้
    อ่านพร้อมๆ กัน

    ว่าการเล่นเกมด้านลูกค้าของผู้เล่นถูกหยุดชั่วคราวในปัจจุบันหรือไม่

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

    บ่งบอกว่าผู้เล่นมีตราที่ได้รับการยืนยันหรือไม่

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

    ตั้งระยะที่ผู้เล่นรายนี้จะเห็นแถบสุขภาพของมนุษย์อื่น ๆ หากตั้งค่าเป็น 0 แถบสุขภาพจะไม่แสดงขึ้น

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

    คุณสมบัตินี้แสดงรหัสท้องถิ่นที่ผู้เล่นท้องถิ่นตั้งไว้สำหรับบัญชี Roblox ของตน

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

    อธิบายประเภทสมาชิกของบัญชี

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

    ตั้งระยะที่ผู้เล่นรายนี้จะเห็นชื่อของ Humanoid อื่น ๆ หากตั้งค่าเป็น 0 ชื่อจะถูกซ่อน

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

    กำหนดว่าผู้เล่นอยู่ในทีมเฉพาะหรือไม่

  • ซ่อนอยู่
    ไม่ซ้ำ
    การรักษาความปลอดภัยของ Roblox
    อ่านพร้อมๆ กัน
  • อ่านพร้อมๆ กัน

    ตั้งค่าส่วนให้โฟกัสการเลียนแบบรอบ

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

    หากตั้งค่าไว้ ผู้เล่นจะเกิดใหม่ที่ SpawnLocation ที่กำหนด

  • การรักษาความปลอดภัยของ Roblox
    อ่านพร้อมๆ กัน
  • ไม่ซ้ำ
    อ่านพร้อมๆ กัน

    กำหนดทีมที่ผู้เล่นเกี่ยวข้องอยู่

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

    กำหนดทีมที่ผู้เล่นเกี่ยวข้องอยู่

  • อ่านอย่างเดียว
    ไม่ซ้ำ
    การรักษาความปลอดภัยของสคริปต์ Roblox
    อ่านพร้อมๆ กัน
  • อ่านพร้อมๆ กัน

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

วิธีการ

  • ลบอุปกรณ์และวัตถุรูปลักษณ์ตัวละครอื่นๆ ทั้งหมดออกจากตัวละครของผู้เล่น

  • คืนระยะห่างระหว่างหัวของตัวละครและจุด Vector3 ที่กำหนด คืน 0 หากผู้เล่นไม่มีตัวละคร

  • คืนสารานุกรมที่มีข้อมูลอธิบายวิธีที่ Player เข้าร่วมประสบการณ์

  • ส่งคืนเมาส์ที่ใช้โดยไคลเอนต์

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

    คืนความล่าช้าของเครือข่ายแยกตัวในวินาที

  • ส่งคืนว่ามีการโหลดตัวละครของผู้เล่นหรือไม่

  • คืนว่าผู้เล่นได้รับการตรวจสอบด้วยสัญญาณที่เป็นรูปธรรมและเป็นจริงหรือไม่

  • Kick(message : string):()

    บังคับให้ตัดการเชื่อมต่อผู้เล่นออกจากเกมโดยมีตัวเลือกในการให้ข้อความ

  • Move(walkDirection : Vector3,relativeToCamera : boolean):()

    ทำให้ตัวละครของผู้เล่นเดินไปในทิศทางที่กำหนดจนกว่าจะหยุดหรือถูกหยุดโดยผู้เล่น (โดยใช้การควบคุมของพวกเขา)

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

    ตั้งค่า AccountAge ของผู้เล่น

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

    ตั้งค่าว่าผู้เล่นจะเห็นการแชทที่กรองหรือไม่ แทนการแชทปกติ

  • ผลตอบแทน

    คืนสารานุกรมของเพื่อนออนไลน์

  • ผลตอบแทน

    ส่งคืนอันดับของผู้เล่นในกลุ่มเป็นจํานวนเต็มระหว่าง 0 และ 255 โดยที่ 0 เป็นสมาชิกและ 255 เป็นเจ้าของกลุ่ม

  • ผลตอบแทน

    ส่งบทบาทของผู้เล่นในกลุ่มเป็นข้อความหรือ "สตริง" ถ้าผู้เล่นไม่ได้เป็นส่วนหนึ่งของกลุ่ม

  • ผลตอบแทน

    ตรวจสอบว่าผู้เล่นเป็นเพื่อนของผู้ใช้ด้วย Player.UserId ที่กำหนด

  • ผลตอบแทน

    ตรวจสอบว่าผู้เล่นเป็นสมาชิกของกลุ่มที่มีรหัสที่กำหนดหรือไม่

  • ผลตอบแทน

    สร้างตัวละครใหม่สำหรับผู้เล่นโดยลบตัวเก่าออก นอกจากนี้ยังล้าง Backpack และ PlayerGui ของผู้เล่น

  • ผลตอบแทน

    สร้างอวตารเพื่อให้มันมีทุกอย่างติดตั้งใน HumanoidDescription ที่ส่งไป

  • RequestStreamAroundAsync(position : Vector3,timeOut : number):()
    ผลตอบแทน

    คำขอที่เซิร์ฟเวอร์สตรีมไปยังผู้เล่นรอบตำแหน่งที่ระบุ

อีเวนต์

  • ยิงเมื่อตัวละครของผู้เล่นเกิดหรือเกิดใหม่

  • เกิดไฟไหม้เมื่อรูปลักษณ์เต็มของ Player.Character ถูกสอดเข้าไป

  • ยิงก่อนที่ตัวละครของผู้เล่นจะถูกลบ

  • Chatted(message : string,recipient : Player):RBXScriptSignal

    เกิดไฟไหม้เมื่อผู้เล่นแชทในเกมโดยใช้แถบแชทที่ Roblox จัดให้

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

  • OnTeleport(teleportState : Enum.TeleportState,placeId : number,spawnName : string):RBXScriptSignal

    ยิงเมื่อสถานะเทเลพอร์ตของผู้เล่นเปลี่ยน

คุณสมบัติ

AccountAge

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

อายุบัญชีเป็นคุณสมบัติ Player ที่อธิบายระยะเวลาที่บัญชีผู้เล่นได้รับการลงทะเบียนไปแล้วในวันมันถูกตั้งค่าโดยใช้ฟังก์ชัน Player:SetAccountAge() ซึ่งไม่สามารถเข้าถึงได้โดยสคริปต์

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

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

This code sample adds a mark to players showing about how old their account is. The mark uses a player's account age to determine if they are a New Player, Veteran Player or Regular Player.

Account Age Mark

local Players = game:GetService("Players")
local MAX_AGE_NEW_PLAYER = 7 -- one week
local MIN_AGE_VETERAN = 365 -- one year
-- This function marks a part with text using a BillboardGui
local function mark(part, text)
local bbgui = Instance.new("BillboardGui")
bbgui.AlwaysOnTop = true
bbgui.StudsOffsetWorldSpace = Vector3.new(0, 2, 0)
bbgui.Size = UDim2.new(0, 200, 0, 50)
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(1, 0, 1, 0) -- Fill parent
textLabel.Text = text
textLabel.TextColor3 = Color3.new(1, 1, 1)
textLabel.TextStrokeTransparency = 0
textLabel.BackgroundTransparency = 1
textLabel.Parent = bbgui
-- Add to part
bbgui.Parent = part
bbgui.Adornee = part
end
local function onPlayerSpawned(player, character)
local head = character:WaitForChild("Head")
if player.AccountAge >= MIN_AGE_VETERAN then
mark(head, "Veteran Player")
elseif player.AccountAge <= MAX_AGE_NEW_PLAYER then
mark(head, "New Player")
else
mark(head, "Regular Player")
end
end
local function onPlayerAdded(player)
-- Listen for this player spawning
if player.Character then
onPlayerSpawned(player, player.Character)
end
player.CharacterAdded:Connect(function()
onPlayerSpawned(player, player.Character)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

AutoJumpEnabled

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

คุณสมบัติ AutoJumpEnabled ตัดสินว่า Player.Character ของ Player โดยใช้อุปกรณ์มือถือจะกระโดดโดยอัตโนมัติเมื่อพวกเขาชนกับสิ่งกีดขวางสิ่งนี้สามารถทำให้ระดับสามารถนําทางได้มากขึ้นขณะอยู่บนอุปกรณ์เคลื่อนที่

เมื่อผู้เล่นเข้าร่วมเกม, ค่า StarterPlayer.AutoJumpEnabled จะกำหนดสถานะเริ่มต้นของคุณสมบัตินี้จากนั้นคุณสมบัตินี้จะกำหนดค่าของคุณสมบัติ Humanoid.AutoJumpEnabled ของ Player.Character ในการสร้างคือการตั้งค่าพฤติกรรมการกระโดดอัตโนมัติบนฐานต่อตัวอักษร ต่อผู้เล่น และต่อเกมในแต่ละครั้งโดยใช้สามคุณสมบัตินี้

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

This code sample is meant for a TextButton. It allows the player to toggle the auto-jumping behavior while on a mobile device.

Auto-Jump Toggle

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent
local function update()
-- Update button text
if player.AutoJumpEnabled then
button.Text = "Auto-Jump is ON"
else
button.Text = "Auto-Jump is OFF"
end
-- Reflect the property in the player's character, if they have one
if player.Character then
local human = player.Character:FindFirstChild("Humanoid")
if human then
human.AutoJumpEnabled = player.AutoJumpEnabled
end
end
end
local function onActivated()
-- Toggle auto-jump
player.AutoJumpEnabled = not player.AutoJumpEnabled
-- Update everything else
update()
end
button.Activated:Connect(onActivated)
update()

CameraMaxZoomDistance

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

คุณสมบัติ CameraMaxZoomDistance Player ตั้งระยะสูงสุดในสตัดที่กล้องสามารถจากตัวละครที่มีกล้องเริ่มต้นได้

คือการควบคุมระยะสูงสุดที่กล้องของผู้เล่นสามารถซูมออกได้

ค่าเริ่มต้นของคุณสมบัตินี้ถูกกำหนดโดย StarterPlayer.CameraMaxZoomDistanceหากค่านี้ถูกตั้งค่าเป็นค่าที่ต่ำกว่า Player.CameraMinZoomDistance ค่าจะถูกเพิ่มเป็นระยะการซูมกล้อง

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

The example demonstrates how to set a player's camera minimum and maximum zoom distance.

In this example, we set the Player.CameraMinZoomDistance and Player.CameraMaxZoomDistance to set the min and max distance in studs a player's camera can be from their character.

Note that since the example attempts to set the CameraMinZoomDistance to be greater than the CameraMaxZoomDistance, the CameraMinZoomDistance value will be decreased and set to the value of the max zoom distance.

To change the default min and max zoom distance values for a player when they first enter the game, you can change the StarterClass.Player.CameraMinZoomDistance and StarterClass.Player.CameraMaxZoomDistance properties.

Setting Camera Zoom Distance

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.CameraMaxZoomDistance = 50
player.CameraMinZoomDistance = 75

CameraMinZoomDistance

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

คุณสมบัติ CameraMinZoonDistance Player ตั้งระยะขั้นต่ำในสตัดที่กล้องสามารถจากตัวละครด้วยกล้องเริ่มต้นได้

คือการควบคุมระยะขั้นต่ำที่กล้องของผู้เล่นสามารถซูมได้

ค่าเริ่มต้นของคุณสมบัตินี้ถูกกำหนดโดย StarterPlayer.CameraMinZoomDistanceหากค่านี้ถูกตั้งเป็นค่าที่สูงกว่า Player.CameraMaxZoomDistance ค่าจะลดลงเป็น CameraMaxZoomDistance

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

The example demonstrates how to set a player's camera minimum and maximum zoom distance.

In this example, we set the Player.CameraMinZoomDistance and Player.CameraMaxZoomDistance to set the min and max distance in studs a player's camera can be from their character.

Note that since the example attempts to set the CameraMinZoomDistance to be greater than the CameraMaxZoomDistance, the CameraMinZoomDistance value will be decreased and set to the value of the max zoom distance.

To change the default min and max zoom distance values for a player when they first enter the game, you can change the StarterClass.Player.CameraMinZoomDistance and StarterClass.Player.CameraMaxZoomDistance properties.

Setting Camera Zoom Distance

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.CameraMaxZoomDistance = 50
player.CameraMinZoomDistance = 75

CameraMode

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

คุณสมบัติ โหมดกล้อง ตั้งค่าโหมดกล้องของผู้เล่นโดยเริ่มต้นเป็นบุคคลที่สาม

คนที่สาม

ในโหมดบุคคลที่สามเริ่มต้น ( Enum.CameraMode.Classic ), ตัวละครสามารถมองเห็นได้ในกล้อง ในขณะที่อยู่ในโหมดนี้ พฤติกรรมเริ่มต้นคือ:

  • ผู้เล่นสามารถคลิกขวาและลาก (เมาส์) แตะและลาก (บนมือถือ) ใช้ thumbstick รอง (เกมแพด) หรือกดลูกศรซ้าย/ขวา (แป้นพิมพ์) เพื่อหมุนกล้องรอบตัวละคร
  • เมื่อผู้เล่นย้ายตัวละครของพวกเขา มันจะเผชิญหน้ากับทิศทางการเคลื่อนที่ที่เหมาะสม
  • ผู้เล่นสามารถซูมเข้าและออกได้อย่างอิสระแม้จนถึงคนแรกที่ซูมเต็มใน

บุคคลแรก

ในโหมดคนแรก ( Enum.CameraMode.LockFirstPerson ) กล้องของผู้เล่นจะซูมตลอดทางเว้นแต่จะมี GUI ที่มองเห็นได้พร้อมกับคุณสมบัติ GuiButton.Modal ที่ตั้งค่าเป็น true เคลื่อนเมาส์ไปยังมือถือหรือใช้สติกเท้าสีที่สองบนคอนโซลเกมจะหมุนกล้องรอบตัวละคร

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

This example demonstrates how to change the character's CameraMode to first person using the LockFirstPerson value of the Enum.CameraMode enum.

Playing in First Person

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.CameraMode = Enum.CameraMode.LockFirstPerson

CanLoadCharacterAppearance

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

คุณสมบัติ CanLoadCharacterAppearance Player จะกำหนดว่าการปรากฏตัวของตัวละครจะถูกโหลดเมื่อผู้เล่นเกิดหรือไม่ค่าเริ่มต้นของคุณสมบัตินี้ถูกกำหนดโดย StarterPlayer.LoadPlayerAppearance

หาก ถูกต้อง ตัวละครจะโหลดรูปลักษณ์ของผู้เล่นที่ตรงกับรูปลักษณ์ของผู้เล่น Player.CharacterAppearanceId

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

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

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

This example demonstrates how to disable loading a player's character appearance. Instead, the player loads as a grey model without any hats, shirts, pants, etc.

This is useful for games using custom clothing and accessories.

Note that if the character has already spawned, this change will not take affect until the player respawns or the Player:LoadCharacter() function is called.

Disabling a Player's Appearance

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.CanLoadCharacterAppearance = false

Character

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

คุณสมบัติ ตัวอักษร มีการอ้างอิงถึง Model ที่มี Humanoid ส่วนตัว สคริปต์ และวัตถุอื่นๆ ที่จำเป็นสำหรับจำลองอวตารของผู้เล่นในประสบการณ์โมเดลจะถูกผูกกับ Workspace แต่อาจถูกย้ายมันจะโหลดโดยอัตโนมัติเมื่อ Players.CharacterAutoLoads เป็น true และสามารถโหลดได้ด้วยตนเองโดยใช้ Player:LoadCharacter() อย่างอื่น

เดิมคุณสมบัตินี้คือ nil และจะถูกตั้งค่าเมื่อตัวละครของผู้เล่นเกิดครั้งแรกใช้อีเวนต์ Player.CharacterAdded เพื่อตรวจสอบเมื่อตัวละครของผู้เล่นโหลดอย่างถูกต้อง และอีเวนต์ Player.CharacterRemoving เพื่อตรวจสอบเมื่อตัวละครกำลังจะหายไปหลีกเลี่ยงการใช้ Object:GetPropertyChangedSignal() บนคุณสมบัตินี้

โปรดทราบว่า LocalScripts ที่ถูกโคลนจาก StarterGui หรือ StarterPack ไปยังผู้เล่น PlayerGui หรือ Backpack ตามลําดับมักจะถูกดําเนินการก่อนที่โมเดลตัวละครเก่าจะถูกแทนที่ ดังนั้น Player.Character อาจหมายถึงโมเดลเก่าที่มีคุณสมบัติ Parent หรือ nilดังนั้นใน LocalScript ภายใต้ StarterGui หรือ StarterPack จึงเป็นการดีที่จะตรวจสอบให้แน่ใจว่าบรรพบุรุษของ ตัวละคร ไม่ได้เป็น nil ก่อนที่จะใช้งาน ตัวอย่างเช่น:


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
if not character or character.Parent == nil then
character = player.CharacterAdded:Wait()
end

CharacterAppearanceId

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

คุณสมบัตินี้กำหนดรหัสผู้ใช้ของบัญชีที่มีลักษณะตัวอักษรที่ใช้สำหรับผู้เล่นของผู้เล่น characterโดยค่าเริ่มต้นคุณสมบัตินี้คือ Player.UserId ซึ่งใช้อวตารของผู้เล่นตามที่พวกเขาได้สร้างไว้บนเว็บไซต์ Roblox

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

เกมยังสามารถสลับได้ว่าตัวละครของผู้เล่นจะโหลดในเกมหรือไม่โดยการเปลี่ยนค่า StarterPlayer.LoadCharacterAppearance คุณสมบัติ

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

This code sample allows players to chat "/disguise xyz", where xyz is a user ID or username, and they will respawn appearing like that account's avatar. Try typing "/disguise 261" or "/disguise Shedletsky"!

Disguise Command

local Players = game:GetService("Players")
local disguiseCommand = "/disguise "
local function onPlayerChatted(player, message)
if message:sub(1, disguiseCommand:len()):lower() == disguiseCommand:lower() then
local input = message:sub(disguiseCommand:len() + 1)
local id = tonumber(input)
if not id then -- Number failed to parse, maybe they typed a username instead
pcall(function() -- This call can fail sometimes!
id = Players:GetUserIdFromNameAsync(input) -- Fetch ID from name
end)
end
if id then
-- Set character appearance then respawn
player.CharacterAppearanceId = id
player:LoadCharacter()
else
-- We couldn't get an ID from their input
end
end
end
local function onPlayerAdded(player)
player.Chatted:Connect(function(...)
onPlayerChatted(player, ...)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

DevCameraOcclusionMode

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

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

ค่าเริ่มต้นคือ Zoom (0) ดู Enum.DevCameraOcclusionMode สำหรับรายการโหมดที่มีอยู่

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

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

ค่าเริ่มต้นของคุณสมบัตินี้จะถูกกำหนดโดย StarterPlayer.DevComputerCameraMovementMode

คําว่า "คอมพิวเตอร์" ในชื่อคุณสมบัตินี้หมายถึงอุปกรณ์ที่ไม่ใช่ TouchEnabled และไม่ใช่ GamepadEnabled

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

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

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

The example demonstrates how to set a player's camera movement mode for players on a computer.

In this example, we set the camera movement mode to Classic via the Enum.DevComputerCameraMovementMode enum. This means that the camera of players on touch enabled devices will track the player but will not automatically rotate if the player walks left or right.

Setting a Player's Camera Movement Mode (Desktop)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Set the player's camera movement mode on computers to classic
player.DevComputerCameraMode = Enum.DevComputerCameraMovementMode.Classic

DevComputerMovementMode

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

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

ค่าเริ่มต้นของคุณสมบัตินี้จะถูกกำหนดโดย StarterPlayer.DevComputerMovementMode

คําว่า "คอมพิวเตอร์" ในชื่อคุณสมบัตินี้หมายถึงอุปกรณ์ที่ไม่ใช่ TouchEnabled

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

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

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

Demonstrates how to set the movement mode for players on computers using the Player.DevComputerMovementMode property.

Setting a Player's Movement Mode (Desktop)

local Players = game:GetService("Players")
local function onPlayerAdded(player: Player)
-- Set the player's movement mode on desktop devices to click-to-move
-- Once set, the player can right click in the game world and the character will move there.
player.DevComputerMovementMode = Enum.DevComputerMovementMode.ClickToMove
end
Players.PlayerAdded:Connect(onPlayerAdded)

DevEnableMouseLock

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

คุณสมบัตินี้กำหนดว่าผู้เล่นสามารถสลับการล็อค Mouse ได้โดยการกด Shift หรือไม่ผู้เล่นสามารถปิดสวิตช์การล็อคเมาส์ในการตั้งค่าเกมของ Roblox ได้โดยค่าเริ่มต้นคุณสมบัตินี้จะถูกตั้งค่าเป็นค่าของ StarterPlayer.EnableMouseLockOptionสิ่งนี้สามารถกำหนดได้ทางด้านเซิร์ฟเวอร์ในระหว่างเวลาทำงานโดยใช้ Scriptไม่สามารถตั้งค่าได้ทางฝั่งไคลเอนต์

เมื่อการล็อคเมาส์เปิดใช้งานแล้ว เคอร์เซอร์ของผู้เล่นจะถูกล็อคไว้ที่ศูนย์กลางของหน้าจอการเคลื่อนเมาส์จะโคจรรอบกล้องรอบผู้เล่น character และตัวละครจะเผชิญหน้ากับทิศทางเดียวกับ cameraนอกจากนี้ยังชดเชยมุมมองของกล้องที่อยู่เหนือไหล่ขวาของตัวละครของผู้เล่น

โปรดทราบว่า API ที่เกี่ยวข้องกับการล็อคเปลี่ยนเกียร์กำลังถูกละทิ้ง ดังนั้นจึงแนะนำให้ใช้ UserInputService.MouseBehavior แทนเพื่อล็อคเมาส์

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

This code sample demonstrates how to toggle whether mouse lock is available to a player using a chat command. When a player types "mouselock", their ability to toggle mouse lock is toggled. Note that this does not toggle the actual state of mouse lock; rather, it changes whether a player is able to toggle it themselves.

This code can be run by pasting it into a Script within ServerScriptService.

Toggling Mouse Lock Ability

local Players = game:GetService("Players")
local function toggleMouseLock(player)
player.DevEnableMouseLock = not player.DevEnableMouseLock
if player.DevEnableMouseLock then
print("Mouse lock is available")
else
print("Mouse lock is not available")
end
end
local function onPlayerChatted(player, message, _recipient)
if message == "mouselock" then
toggleMouseLock(player)
end
end
local function onPlayerAdded(player)
player.Chatted:Connect(function(...)
onPlayerChatted(player, ...)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
อ่านพร้อมๆ กัน

คุณสมบัติ DevTouchCameraMode กำหนดวิธีที่ผู้เล่นย้ายกล้องเมื่อใช้อุปกรณ์ TouchEnabledดู Enum.DevTouchCameraMovementMode สำหรับคำอธิบายของโหมดการควบคุมกล้องที่มีอยู่ทั้งหมดคุณสมบัตินี้ไม่สามารถตั้งค่าได้โดยใช้ LocalScript (ต้องตั้งค่าบนเซิร์ฟเวอร์โดยใช้ Script )

ค่าเริ่มต้นของคุณสมบัตินี้จะถูกกำหนดโดย StarterPlayer.DevTouchCameraMovementMode

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

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

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

The example demonstrates how to set a player's camera movement mode.

In this example, we set the camera movement mode to Classic via the Enum.DevTouchCameraMovementMode enum. This means that the camera of players on touch enabled devices will track the player but will not automatically rotate if the player walks left or right.

Setting a Player's Camera Movement Mode (Touch)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Set the player's camera movement mode on mobile devices to classic
player.DevTouchCameraMovementMode = Enum.DevTouchCameraMovementMode.Classic

DevTouchMovementMode

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

คุณสมบัติ DevTouchMovementMode กำหนดวิธีที่ผู้เล่นย้ายตัวละครเมื่อใช้อุปกรณ์ TouchEnabledดู Enum.DevTouchMovementMode สำหรับคำอธิบายของแต่ละโหมดการควบคุมการเคลื่อนไหวที่มีอยู่คุณสมบัตินี้ไม่สามารถตั้งค่าได้โดยใช้ LocalScript (ต้องตั้งค่าบนเซิร์ฟเวอร์โดยใช้ Script )

ค่าเริ่มต้นของคุณสมบัตินี้จะถูกกำหนดโดย StarterPlayer.DevTouchMovementMode

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

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

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

The example demonstrates how to set the movement mode for players on touch enabled devices.

In this example, we set the movement mode to Thumbstick via the Enum.DevTouchMovementMode enum. This means that players on touch enabled devices are able to move via a virtual thumbstick on their screen.

Setting a Player's Movement Mode (Touch)

local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
-- Set the player's movement mode on mobile devices to a dynamic thumbstick
player.DevTouchMovementMode = Enum.DevTouchMovementMode.DynamicThumbstick
end)

DisplayName

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

The DisplayName เป็นคุณสมบัติ Player ที่มีชื่อแสดงของผู้ใช้ที่ได้รับการยืนยันที่เกี่ยวข้องกับวัตถุ Playerไม่เหมือนชื่อผู้ใช้ชื่อแสดงเป็นชื่อที่ไม่ซ้ำกันที่ผู้เล่นแสดงให้กับผู้อื่นหากผู้ใช้ Roblox ยังไม่ได้เลือกอันใด คุณสมบัติจะอ่านเหมือนกับคุณสมบัติ Name

หมายเหตุ:

  • เนื่องจากชื่อที่แสดงไม่ได้เป็นเอกลักษณ์ จึงเป็นไปได้ที่ผู้เล่นสองคนในอินสแตนซ์เดียวจะมีชื่อเหมือนกันหากต้องการระบุตัวระบุที่ไม่ซ้ำกันทั่วโลกสำหรับผู้เล่น ให้ใช้ Player.UserId (ซึ่งเป็นคงที่) หรือ Player.Name (ซึ่งเป็นชื่อผู้ใช้ปัจจุบัน) แทน
  • ตัวละครที่สร้างด้วย Player.LoadCharacter หรือโดยเครื่องยนต์ Roblox จะมีคุณสมบัติ Humanoid.DisplayName ถูกกำหนดให้กับคุณสมบัติ Player.DisplayName
  • ชื่อที่แสดงอาจมีตัวอักษรยูนิโค้ดในสตริง ดู UTF-8 สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการทำงานกับสตริงที่มีตัวอักษรยูนิโค้ด

FollowUserId

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

FollowUserId เป็นคุณสมบัติ Player ที่มีค่า Player.UserId ของผู้ใช้ที่ผู้เล่นติดตามเข้าสู่เกมหากผู้เล่นไม่ทําตามใครสู่เกม คุณสมบัตินี้จะเป็น 0คุณสมบัตินี้มีประโยชน์สำหรับการเตือนผู้เล่นที่ถูกติดตามโดยผู้เล่นคนอื่นเข้าสู่เกม

คุณสามารถรับชื่อของผู้เล่นที่ติดตามโดยใช้รหัสผู้ใช้นี้และฟังก์ชัน Players:GetNameFromUserIdAsync()

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

This code sample alerts players if a new player follows the local player into the game. Place this in a LocalScript in StarterPlayerScripts.

Followed Alert

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = player:WaitForChild("PlayerGui")
local function onPlayerAdded(newPlayer)
if newPlayer.FollowUserId == player.UserId then
local textLabel = Instance.new("TextLabel")
textLabel.Parent = screenGui
textLabel.Text = "You were followed to this game by " .. newPlayer.Name .. "!"
task.delay(3, function()
if textLabel then
textLabel:Destroy()
end
end)
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

GameplayPaused

การรักษาความปลอดภัยที่เข้าถึงไม่ได้
อ่านพร้อมๆ กัน

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

ดูเพิ่ม:

  • Workspace.StreamingEnabled ซึ่งควบคุมว่าการสตรีมเนื้อหาจะเปิดใช้งานหรือไม่
  • Workspace.StreamingIntegrityMode และ Enum.StreamingIntegrityMode สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับเมื่อการเล่นเกมถูกหยุดชั่วคราว

HasVerifiedBadge

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

คุณสมบัติ HasVerifiedBadge Player บ่งบอกว่าผู้เล่นมีตราสัญลักษณ์ที่ได้รับการยืนยันหรือไม่

HealthDisplayDistance

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

คุณสมบัติ HealthDisplayDistance ตั้งระยะห่างในสตัดที่ผู้เล่นคนนี้จะเห็นแถบสุขภาพอื่น ๆ ได้หากตั้งค่าเป็น 0 แถบสุขภาพจะไม่แสดงขึ้นคุณสมบัตินี้ถูกตั้งค่าเป็น StarterPlayer.HealthDisplayDistance โดยค่าเริ่มต้น

หากแถบสุขภาพของมนุษย์หุ่นยนต์มองเห็นได้ คุณสามารถตั้งประเภทการแสดงผลโดยใช้ Humanoid.DisplayDistanceType ได้

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

This example demonstrates how to hide other Humanoid's (Player and NPC) health bars and names.

This is done by setting the player's Player.HealthDisplayDistance and Player.NameDisplayDistance properties to 0.

If you would like to display health bars and names, you set the properties to a value greater than 0. For instance, setting the properties to 100 means that the player will see other player's health and names up to 100 studs away.

To modify the default values for players, you can change the values of the StarterClass.Player.HealthDisplayDistance and StarterClass.Player.NameDisplayDistance properties.

Hiding Player Health and Names

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.HealthDisplayDistance = 0
player.NameDisplayDistance = 0

LocaleId

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

คุณสมบัติ LocaleId Player แสดงรหัสภูมิภาคที่ผู้เล่นท้องถิ่นได้ตั้งค่าไว้สำหรับบัญชี Roblox ของตนมันเก็บสตริงที่มีรหัสตัวอักษรสองตัว (เช่น "en-us") สำหรับภูมิภาค

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

ดูเพิ่มเติม LocalizationService.RobloxLocaleId , ID ท้องถิ่นที่ใช้สําหรับการแปลงเนื้อหาภายในนี่จะเป็นค่าที่แตกต่างเมื่อ Roblox ยังไม่สนับสนุนชุดตั้งค่าท้องถิ่นของผู้เล่นท้องถิ่นภายในองค์กร

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

This example demonstrates how to check the locale of a local player using the Player.LocaleId property. It prints a string with the two letter locale code for the locale of the local player.

For instance, if the player's local is within the US, the locale will be:

en-us

Checking a Player's Locale

local Players = game:GetService("Players")
local player = Players.LocalPlayer
print(player.LocaleId)

MembershipType

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

คุณสมบัตินี้สามารถอ่านได้เพื่อกำหนดสิทธิ์การเป็นสมาชิก (พิมพ์)มันมี enum Enum.MembershipType พิมพ์

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

The following example checks whether a player has Premium membership.

Check Player Membership Status

local Players = game:GetService("Players")
local player = Players.LocalPlayer
if player.MembershipType == Enum.MembershipType.Premium then
-- Take some action specifically for Premium members
end

NameDisplayDistance

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

คุณสมบัติ NameDisplayDistance StarterPlayer ตั้งระยะห่างในสตัดที่ผู้เล่นรายนี้จะเห็นชื่ออื่น ๆ Humanoid ได้หากคุณสรรพ์ถูกตั้งค่าเป็น 0 ชื่อจะถูกซ่อนคุณสมบัตินี้ถูกตั้งค่าเป็น StarterPlayer.NameDisplayDistance โดยค่าเริ่มต้น

หากแถบสุขภาพของมนุษย์หุ่นยนต์มองเห็นได้ คุณสามารถตั้งประเภทการแสดงผลโดยใช้ Humanoid.DisplayDistanceType ได้

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

This example demonstrates how to hide other Humanoid's (Player and NPC) health bars and names.

This is done by setting the player's Player.HealthDisplayDistance and Player.NameDisplayDistance properties to 0.

If you would like to display health bars and names, you set the properties to a value greater than 0. For instance, setting the properties to 100 means that the player will see other player's health and names up to 100 studs away.

To modify the default values for players, you can change the values of the StarterClass.Player.HealthDisplayDistance and StarterClass.Player.NameDisplayDistance properties.

Hiding Player Health and Names

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.HealthDisplayDistance = 0
player.NameDisplayDistance = 0

Neutral

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

คุณสมบัติเป็นกลางจะกำหนดว่าผู้เล่นอยู่ในทีมเฉพาะหรือไม่

  • เมื่อ จริง ผู้เล่นไม่ได้อยู่ในทีมเฉพาะซึ่งหมายความว่าคุณสมบัติ จะเป็นสีขาวและคุณสมบัติ จะเป็นสีขาว
  • เมื่อ ไม่ถูก ผู้เล่นอยู่ในทีมเฉพาะคุณสมบัติ Player.Team จะตรงกับ Team ที่ผู้เล่นอยู่ เช่นเดียวกับ Player.TeamColor

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

This example checks if a player is neutral. If the player is neutral, the game prints:

Player is neutral!

If the player is not neutral, the game prints:

Player is not neutral!

Note: Although this example prints the value of the local player's neutral property, you can change the example to get the value for any player.

Checking if a Player is Neutral

local Players = game:GetService("Players")
local player = Players.LocalPlayer
if player.Neutral then
print("Player is neutral!")
else
print("Player is not neutral!")
end

PartyId

ซ่อนอยู่
ไม่ซ้ำ
การรักษาความปลอดภัยของ Roblox
อ่านพร้อมๆ กัน

ReplicationFocus

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

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

เมื่อคุณสมบัตินี้เป็น nil มันจะกลับสู่พฤติกรรมเริ่มต้นซึ่งคือการปฏิบัติต่อตัวละครของผู้เล่นท้องถิ่น PrimaryPart เป็นจุดโฟกัสการเลียนแบบ

คุณสมบัตินี้ควรตั้งค่าเฉพาะบนเซิร์ฟเวอร์ด้วย Script ไม่ใช่ LocalScriptโปรดทราบว่าคุณสมบัตินี้ไม่เปลี่ยนหรืออัปเดตการเป็นเจ้าของเครือข่ายของชิ้นส่วน

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

This example creates a new BasePart and sets the Player's Player.ReplicationFocus to that part.

This demonstrates the functionality of the ReplicationFocus property. You can easily change the part that the focus is set to to change the replication focus.

Setting a Player's Replication Focus

local Players = game:GetService("Players")
local PLAYER_NAME = "polarpanda16"
local player = Players:WaitForChild(PLAYER_NAME)
local part = Instance.new("Part")
part.Parent = workspace
part.Name = "ReplicationFocusPart"
part.Anchored = true
player.ReplicationFocus = part

RespawnLocation

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

หากตั้งค่าไว้ ผู้เล่นจะเกิดใหม่ที่ SpawnLocation ที่กำหนดคุณสมบัตินี้สามารถตั้งค่าได้ผ่าน Luau และต้องมีการอ้างอิงถึง SpawnLocation ที่ถูกต้องซึ่งต้องตรงกับเกณฑ์ต่อไปนี้:

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

ทางเลือกสำหรับ RespawnLocation
  • A Player จะเกิดจาก SpawnLocations ที่เป็นของทีมของพวกเขา ในบางกรณีอาจเป็นเรื่องง่ายกว่าที่จะเปลี่ยน Player.Team ของผู้เล่นแทน
  • ใช้ เพื่อสร้างโลจิสต์การเกิดที่กำหนดเองของคุณเองโดยใช้ เพื่อย้าย ด้วยตนเอง

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

This code sample will set the player to always respawn from the last SpawnLocation they touched. New players will respawn from the SpawnLocation named 'FirstSpawn' until they touch a different SpawnLocation.

This is an alternative to using the AllowTeamChangeOnTouch property to switch SpawnLocations and does not require Teams.

Change Spawn on Touch

local Players = game:GetService("Players")
local function addSpawn(spawnLocation)
-- listen for the spawn being touched
spawnLocation.Touched:Connect(function(hit)
local character = hit:FindFirstAncestorOfClass("Model")
if character then
local player = Players:GetPlayerFromCharacter(character)
if player and player.RespawnLocation ~= spawnLocation then
local humanoid = character:FindFirstChildOfClass("Humanoid")
-- make sure the character isn't dead
if humanoid and humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
print("spawn set")
player.RespawnLocation = spawnLocation
end
end
end
end)
end
local firstSpawn
-- look through the workspace for spawns
for _, descendant in pairs(workspace:GetDescendants()) do
if descendant:IsA("SpawnLocation") then
if descendant.Name == "FirstSpawn" then
firstSpawn = descendant
end
addSpawn(descendant)
end
end
local function playerAdded(player)
player.RespawnLocation = firstSpawn
end
-- listen for new players
Players.PlayerAdded:Connect(playerAdded)
-- go through existing players
for _, player in pairs(Players:GetPlayers()) do
playerAdded(player)
end

StepIdOffset

การรักษาความปลอดภัยของ Roblox
อ่านพร้อมๆ กัน

Team

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

คุณสมบัติทีมเป็นการอ้างอิงถึงวัตถุ Team ภายในบริการ Teamsมันกำหนดทีมที่ผู้เล่นอยู่; หาก Player ไม่ได้อยู่ในทีมหรือมี Player.TeamColor ตั้งค่าคุณสมบัตินี้จะเป็น nilเมื่อคุณสมบัตินี้ถูกกำหนด ผู้เล่นจะเข้าร่วมกิจกรรม Team และกิจกรรม Team.PlayerAdded บนทีมที่เกี่ยวข้องเช่นเดียวกัน, Team.PlayerRemoved ไฟเมื่อคุณสมบัติถูกปิดใช้งานจากบาง Team

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

This code sample, although lengthy, is quite simple: detect when a player chats /play, then put them on the "Playing" team. When they die, move them back to the "Spectating" team.

Playing/Spectating Teams

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local teamPlaying = Teams.Playing
local teamSpectators = Teams.Spectating
local playCommand = "/play"
local function play(player)
player.Team = teamPlaying
player.TeamColor = teamPlaying.TeamColor
-- Respawn the player (moves them to spawn location)
player:LoadCharacter()
end
local function onPlayerDied(player, _character)
-- When someone dies, put them on the spectator team
player.Team = teamSpectators
end
local function onPlayerSpawned(player, character)
local human = character:WaitForChild("Humanoid")
human.Died:Connect(function()
onPlayerDied(player, character)
end)
end
local function onPlayerChatted(player, message)
if message:sub(1, playCommand:len()):lower() == playCommand then
play(player)
end
end
local function onPlayerAdded(player)
if player.Character then
onPlayerSpawned(player, player.Character)
end
player.CharacterAdded:Connect(function()
onPlayerSpawned(player, player.Character)
end)
player.Chatted:Connect(function(message, _recipient)
onPlayerChatted(player, message)
end)
end
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

This code sample allows any player to chat "/jointeam " where is the name of a team. It uses string manipulation using string.sub and string.lower to make the command case-insensitive and allow for partial matches. For example, "/jointeam red" will match the team "Red Robins".

Join Team Command

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
-- Command to choose a team (note the trailing space)
local joinCommand = "/jointeam "
local function findTeamByName(name)
-- First, check for the exact name of a team
if Teams:FindFirstChild(name) then
return Teams[name]
end
-- Let's check for case-insensitive partial matches, like "red" for "Red Robins"
for _, team in pairs(Teams:GetChildren()) do
if team.Name:sub(1, name:len()):lower() == name:lower() then
return team
end
end
-- If we get to this point, no team matched the one we were looking for :(
end
local function onPlayerChatted(player, message, _recipient)
-- Note: string.sub(message, ...) is the same as message:sub(...)
if message:sub(1, joinCommand:len()):lower() == joinCommand:lower() then
-- Matched "/JOINTEAM xyz" to our join command prefix "/jointeam "
local teamName = message:sub(joinCommand:len() + 1) -- Cut out the "xyz" from "/jointeam xyz"
local team = findTeamByName(teamName)
if team then
-- Set the team!
player.Team = team
player.Neutral = false
else
-- Tell the player that team could not be found :(
player.Team = nil
player.Neutral = true
end
end
end
local function onPlayerAdded(player)
player.Chatted:Connect(function(...)
onPlayerChatted(player, ...)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

TeamColor

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

คุณสมบัติ TeamColor กำหนดทีมที่ผู้เล่นเกี่ยวข้องกับตามทีมนั้น Team.TeamColor ของทีมการเปลี่ยนค่านี้จะเปลี่ยน Player.Team ตามทีมที่มีค่าเดียวกัน BrickColor สำหรับ Team.TeamColor ของพวกเขาหากไม่มีวัตถุทีมใดมีสีทีมที่เกี่ยวข้อง ผู้เล่นจะไม่ถูกเชื่อมโยงกับทีม

มักเป็นความคิดที่ดีกว่าที่จะตั้ง Player.Team ให้เป็น Team แทนที่จะใช้คุณสมบัตินี้การตั้งค่าคุณสมบัตินี้มักจะนำไปสู่การทำซ้ำมูลค่า BrickColor เดียวกันสำหรับทีมเฉพาะในหลายสคริปต์; นี่คือสิ่งที่คุณต้องการหลีกเลี่ยงเมื่อปฏิบัติตามหลักการ "อย่าทำซ้ำตัวเอง" (DRY)

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

This code sample, although lengthy, is quite simple: detect when a player chats /play, then put them on the "Playing" team. When they die, move them back to the "Spectating" team.

Playing/Spectating Teams

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local teamPlaying = Teams.Playing
local teamSpectators = Teams.Spectating
local playCommand = "/play"
local function play(player)
player.Team = teamPlaying
player.TeamColor = teamPlaying.TeamColor
-- Respawn the player (moves them to spawn location)
player:LoadCharacter()
end
local function onPlayerDied(player, _character)
-- When someone dies, put them on the spectator team
player.Team = teamSpectators
end
local function onPlayerSpawned(player, character)
local human = character:WaitForChild("Humanoid")
human.Died:Connect(function()
onPlayerDied(player, character)
end)
end
local function onPlayerChatted(player, message)
if message:sub(1, playCommand:len()):lower() == playCommand then
play(player)
end
end
local function onPlayerAdded(player)
if player.Character then
onPlayerSpawned(player, player.Character)
end
player.CharacterAdded:Connect(function()
onPlayerSpawned(player, player.Character)
end)
player.Chatted:Connect(function(message, _recipient)
onPlayerChatted(player, message)
end)
end
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

ThirdPartyTextChatRestrictionStatus

อ่านอย่างเดียว
ไม่ซ้ำ
การรักษาความปลอดภัยของสคริปต์ Roblox
อ่านพร้อมๆ กัน

UserId

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

UserId เป็นคุณสมบัติ ที่มีตัวเลขอ่านได้ที่เฉพาะและสม่ำเสมอที่ระบุบัญชีผู้ใช้ทุกบัญชีบน Robloxไม่เหมือนกับ Instance.Name ของผู้เล่นซึ่งอาจเปลี่ยนตามชื่อผู้ใช้ปัจจุบันของผู้ใช้ ค่านี้จะไม่เปลี่ยนเป็นค่าเดียวกันสำหรับบัญชีเดียวกัน

คุณสมบัตินี้จำเป็นเมื่อบันทึก/โหลดข้อมูลผู้เล่นโดยใช้ GlobalDataStoresใช้ UserId คีย์

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

The below example would print the UserId of every user who entered a game.

Player.UserId

local Players = game:GetService("Players")
local function onPlayerAdded(player)
print(player.UserId)
end
Players.PlayerAdded:Connect(onPlayerAdded)
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

The following code sample gives an example of a 'met the creator' badge system. This script will award a specified badge (BADGE_ID) to anyone who is in a server at the same time as the user associated with OWNER_ID.

Met the Creator Badge

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local OWNER_ID = 212423 -- can use game.CreatorId for published places
local BADGE_ID = 1
local ownerInGame = false
local function playerAdded(newPlayer)
if newPlayer.UserId == OWNER_ID then
-- if new player is the owner, set ownerInGame to true and give everyone the badge
ownerInGame = true
for _, player in pairs(Players:GetPlayers()) do
-- don't award the owner
if player ~= newPlayer then
BadgeService:AwardBadge(player.UserId, BADGE_ID)
end
end
elseif ownerInGame then
-- if the owner is in the game, award the badge
BadgeService:AwardBadge(newPlayer.UserId, BADGE_ID)
end
end
local function playerRemoving(oldPlayer)
if oldPlayer.UserId == OWNER_ID then
ownerInGame = false
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoving)

This code sample retrieves a player's saved gold from a data store and puts the returned value onto the leaderboard. Note that this sample does not save players' gold — it only loads it.

Data Store to Leaderboard

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local goldDataStore = DataStoreService:GetDataStore("Gold")
local STARTING_GOLD = 100
local function onPlayerAdded(player)
local playerKey = "Player_" .. player.UserId
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Parent = leaderstats
local success, result = pcall(function()
return goldDataStore:GetAsync(playerKey) or STARTING_GOLD
end)
if success then
gold.Value = result
else
-- Failed to retrieve data
warn(result)
end
leaderstats.Parent = player
end
Players.PlayerAdded:Connect(onPlayerAdded)

วิธีการ

AddReplicationFocus

()

พารามิเตอร์

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

ส่งค่ากลับ

()

ClearCharacterAppearance

()

ฟังก์ชัน ClearCharacterAppearance ลบทั้งหมด Accessory , Shirt , Pants , CharacterMesh และ BodyColors จากผู้เล่นที่กำหนด Player.Characterนอกจากนี้ยังลบเสื้อยืด Decal บนท่อนของผู้เล่นส่วนสีของร่างกายและใบหน้าของตัวละครจะไม่เปลี่ยนแปลงวิธีนี้ไม่ทำอะไรถ้าผู้เล่นไม่มีตัวละคร

มันไม่ลบ t-shirts , เมชที่หัว, หรือใบหน้า


ส่งค่ากลับ

()

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

How to Clear a Character's Appearance

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local function onChildRemoved(child)
print(child.ClassName, "removed from character")
end
character.ChildRemoved:Connect(onChildRemoved)
player:ClearCharacterAppearance()
--> BodyColors removed from character
--> ShirtGraphic removed from character
--> Shirt removed from character
--> Pants removed from character
--> CharacterMesh removed from character
--> Hat removed from character
--> Shirt removed from character

DistanceFromCharacter

ฟังก์ชัน DistanceFromCharacter Player คือการคืนระยะห่างระหว่างหัวของตัวละครและจุดที่กำหนด Vector3มันส่งคืน 0 ถ้าผู้เล่นไม่มี Player.Character

มีประโยชน์เมื่อกำหนดระยะห่างระหว่างผู้เล่นกับวัตถุหรือสถานที่อื่นๆ ในเกม

หากคุณต้องการกำหนดระยะห่างระหว่างสองตัวอย่างหรือตำแหน่งที่ไม่ใช่ผู้เล่น กำลังติดตาม:


local distance = (position1 - position2).Magnitude

พารามิเตอร์

point: Vector3

ตำแหน่งที่ระยะห่างจากผู้เล่นถูกวัด

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

ส่งค่ากลับ

ระยะห่างในสตัดระหว่างผู้เล่นและตำแหน่ง

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

This example demonstrates how to measure the distance between a player's Player.Character and another location.

This code will print the distance of each player's character from the origin (0, 0, 0):

Measuring the Distance Between a Player and a Position

local Players = game:GetService("Players")
for _, player in pairs(Players:GetPlayers()) do
print(player:DistanceFromCharacter(Vector3.new(0, 0, 0)))
end

GetJoinData

คืนสารานุกรมที่มีข้อมูลอธิบายวิธีที่ผู้เล่นเข้าร่วมประสบการณ์ สารานุกรมมีฟิลด์ใด ๆ ต่อไปนี้:


<th>ประเภทค่า</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tbody>
<tr>
<th>รหัสเกมที่มา</th>
<td>จํานวน</td>
<td>คลาส <code>Class.DataModel.GameId</code> ของประสบการณ์ที่ <code>ผู้เล่น</code> เทเลพอร์ตมาจากปรากฏเฉพาะเมื่อผู้เล่นเทเลพอร์ตไปยังประสบการณ์ปัจจุบันและหากเซิร์ฟเวอร์เรียกฟังก์ชันการเทเลพอร์ต</td>
</tr>
<tr>
<th>รหัสสถานที่ที่มา</th>
<td>จํานวน</td>
<td>คลาส <code>Class.DataModel.PlaceId</code> ของสถานที่ที่ <code>ผู้เล่น</code> เทเลพอร์ตมาปรากฏเฉพาะเมื่อผู้เล่นเทเลพอร์ตไปยังสถานที่ปัจจุบันและเซิร์ฟเวอร์เรียกฟังก์ชันการเทเลพอร์ต</td>
</tr>
<tr>
<th>อ้างอิงโดย PlayerId</th>
<td>จํานวน</td>
<td>คลาส <code>Class.Player.UserId</code> ของผู้เล่นที่เชิญผู้เล่นปัจจุบันไปยังประสบการณ์ใช้ข้อมูลนี้เพื่อระบุผู้อ้างอิงและเรียกการโต้ตอบของระบบรางวัล</td>
</tr>
<tr>
<th>สมาชิก</th>
<td>รายการ</td>
<td>คอลเลกชันที่มีหมายเลข <code>Class.Player.UserId</code> ของผู้ใช้ที่เทเลพอร์ตไปพร้อมกับ <code>ผู้เล่น</code>ปรากฏเฉพาะเมื่อผู้เล่นเทเลพอร์ตเป็นส่วนหนึ่งของกลุ่ม</td>
</tr>
<tr>
<th>เทเลพอร์ตข้อมูล</th>
<td>ตัวแปร</td>
<td>สะท้อนข้อมูลเทเลพอร์ต <code>teleportData</code> ที่ระบุไว้ในการเทเลพอร์ตเดิมมีประโยชน์สำหรับการแบ่งปันข้อมูลระหว่างเซิร์ฟเวอร์ที่ผู้เล่นเทเลพอร์ตไปปรากฏเฉพาะถ้า <code>teleportData</code> ถูกระบุและเซิร์ฟเวอร์เรียกฟังก์ชันการเทเลพอร์ต</td>
</tr>
<tr>
<th>ข้อมูลการเปิดตัว</th>
<td>สตริง</td>
<td>สตริงที่เข้ารหัสแบบธรรมดาหรือ JSON ที่มีข้อมูลการเปิดตัวที่ระบุไว้ใน <a href="/production/promotion/deeplinks">ลิงก์ลึก</a> URL หรือ <code>Class.ExperienceInviteOptions.LaunchData</code></td>
</tr>
<tr>
<th>เกมJoinContext</th><td>สารานุกรม</td>
<td>
สารานุกรมที่มีข้อมูลที่เกี่ยวข้องตามบริบทของการเข้าร่วมเกม ประกอบด้วยคีย์ต่อไปนี้:
<ul>
<li><code>JoinSource</code> : <code>Enum.JoinSource</code></li><li><code>ItemType</code> : ไม่บังคับ <code>Enum.AvatarItemType</code></li><li><code>AssetId</code> : ไม่บังคับ <code>string</code> ></li><li><code>OutfitId</code> : ไม่บังคับ <code>string</code> ></li><li><code>AssetType</code> : ไม่บังคับ <code>Enum.AssetType</code> ></li>
</ul>
</td>
</tr>
</tbody>
กุญแจ

รับข้อมูลเข้าร่วมและข้อมูลการเทเลพอร์ต

หากเซิร์ฟเวอร์เริ่มการเทเลพอร์ตของผู้เล่น คลังข้อมูลที่วิธีนี้ส่งคืนรวมถึงข้อมูลการเทเลพอร์ตของผู้เล่นวิธี Player:GetJoinData() สามารถใช้เพื่อดึงข้อมูลการเทเลพอร์ตบนเซิร์ฟเวอร์ได้เท่านั้นเพื่อดึงข้อมูลบนไคลเอนต์ใช้ TeleportService:GetLocalPlayerTeleportData()

ไม่เหมือนกับ TeleportService:GetLocalPlayerTeleportData() , Player:GetJoinData() เพียงให้ข้อมูลการเทเลพอร์ตที่ตรงกับเกณฑ์ความปลอดภัยต่อไปนี้:

  • รับประกันว่าถูกส่งโดยเซิร์ฟเวอร์ Roblox ในช่วง 48 ชั่วโมงที่ผ่านมา
  • มันได้รับการรับประกันว่าถูกส่งด้วย Player
  • The SourcePlaceId และ SourceGameId รับประกันว่าเป็นสถานที่และจักรวาลที่ข้อมูลถูกส่งมาซึ่งหมายความว่าคุณสามารถตรวจสอบข้อมูลการเทเลพอร์ตที่มาจากสถานที่ที่ได้รับการอนุมัติ

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


ส่งค่ากลับ

สารานุกรมที่มีค่า PlaceId และ UserId (ดูตารางในคำอธิบาย)

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

The following example tracks sources of traffic for analytics. By creating URLs with unique launch data for each social platform, you can determine the most popular traffic sources. The sample checks the source against a list of possible samples and discards any invalid sources because users can modify the launch data.

Tracking Traffic Sources

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local analyticsStore = DataStoreService:GetDataStore("Analytics")
local ALLOWED_SOURCES = {
"twitter",
"youtube",
"discord",
}
local function onPlayerAdded(player)
local source = player:GetJoinData().LaunchData
-- check if the provided source is valid
if source and table.find(ALLOWED_SOURCES, source) then
-- update the data store to track the source popularity
local success, result = pcall(analyticsStore.IncrementAsync, analyticsStore, source)
if success then
print(player.Name, "joined from", source, "- total:", result)
else
warn("Failed to record join source: " .. result)
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

The following example generates a URL with the user's ID used as launch data. It then displays the URL in a read-only text box that makes it easy for the user to copy and share the link with their friends. When a user joins the game using a referral link, you can use the launch data to reward the referrer.

Referral URL Generator

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local DIRECT_JOIN_URL = "https://www.roblox.com/games/start?placeId=%d&launchData=%s"
local textBox = script.Parent
local function generateReferralURL(player)
return DIRECT_JOIN_URL:format(game.PlaceId, player.UserId)
end
local function highlightAll()
if -- avoid recursive property updates
textBox:IsFocused() and not (textBox.SelectionStart == 1 and textBox.CursorPosition == #textBox.Text + 1)
then
textBox.SelectionStart = 1
textBox.CursorPosition = #textBox.Text + 1
end
end
textBox.Focused:Connect(highlightAll)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(highlightAll)
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(highlightAll)
textBox.TextEditable = false
textBox.ClearTextOnFocus = false
textBox.Text = generateReferralURL(player)

The following example is a function that converts a table into a string you can use as launch data. The provided data is JSON encoded, checked for valid character length, and escaped with percent signs.

Using a Table as Launch Data

local HttpService = game:GetService("HttpService")
local DATA_CHARACTER_LIMIT = 200
local function encodeTableAsLaunchData(data)
-- convert the table to a string
local jsonEncodedData = HttpService:JSONEncode(data)
if #jsonEncodedData <= DATA_CHARACTER_LIMIT then
-- escape potentially invalid characters, such as spaces
local urlEncodedData = HttpService:UrlEncode(jsonEncodedData)
return true, urlEncodedData
else
-- report character limit error
return false, ("Encoded table exceeds %d character limit"):format(DATA_CHARACTER_LIMIT)
end
end
local sampleData = {
joinMessage = "Hello!",
urlCreationDate = os.time(),
magicNumbers = {
534,
1337,
746733573,
},
}
local success, encodedData = encodeTableAsLaunchData(sampleData)
if success then
print(encodedData)
else
warn("failed to encode launch data: " .. encodedData)
end

The following example attempts to decode launch data, using pcall to prevent an error in case the data is corrupt.

Decoding JSON Launch Data

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local function onPlayerAdded(player)
local launchData = player:GetJoinData().LaunchData
if launchData then
-- attempt to decode the data
local success, result = pcall(HttpService.JSONDecode, HttpService, launchData)
if success then
print(player.Name, "joined with data:", result)
else
-- this is probably due to the user messing with the URL
warn("Failed to parse launch data:" .. result)
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

The following code sample is an example of how teleport data can be retrieved on the server using Player:GetJoinData(). This code, when ran in a Script in ServerScriptService, will listen for new Player|Players joining the game. When they join it will retrieve their teleport data (verifying it came from a valid place) to find their current level.

Server TeleportData Example

local Players = game:GetService("Players")
local approvedPlaceIds = { 1 } -- insert approved PlaceIds here
local function isPlaceIdApproved(placeId)
for _, id in pairs(approvedPlaceIds) do
if id == placeId then
return true
end
end
return false
end
local function onPlayerAdded(player)
local joinData = player:GetJoinData()
-- verify this data was sent by an approved place
if isPlaceIdApproved(joinData.SourcePlaceId) then
local teleportData = joinData.TeleportData
if teleportData then
local currentLevel = teleportData.currentLevel
print(player.Name .. " is on level " .. currentLevel)
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

GetMouse

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

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

หมายเหตุ:

  • รายการนี้ ต้องใช้ ใน LocalScript เพื่อทำงานตามที่คาดไว้ออนไลน์
  • หลังจากการอัปเดตในเดือนกรกฎาคม 2014 ไอคอนของเมาส์สามารถตั้งค่าได้ด้วยวิธีนี้

ส่งค่ากลับ

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

The below example will print:

Button 1 is down

whenever the Players.LocalPlayer left clicks.

How to Track Mouse Input

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onButton1Down()
print("Button 1 is down")
end
mouse.Button1Down:Connect(onButton1Down)

GetNetworkPing

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

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

สำหรับด้านไคลเอนต์ LocalScripts ฟังก์ชันนี้สามารถโทรได้เฉพาะใน Players.LocalPlayerฟังก์ชันนี้มีประโยชน์ในการระบุและแก้ไขปัญหาที่เกิดขึ้นในสถานการณ์ความล่าช้าของเครือข่ายสูงยังมีประโยชน์สำหรับการปกปิดความล่าช้า เช่น ปรับความเร็วของแอนิเมชั่นการโยนสำหรับกระสุน


ส่งค่ากลับ

HasAppearanceLoaded

ฟังก์ชัน HasAppearanceLoaded Player จะคืนว่ามีการโหลดรูปลักษณ์ของผู้เล่น Player.Character หรือไม่

รูปลักษณ์ของผู้เล่นรวมถึงไอเทมเช่น ผู้เล่น Shirt , Pants และ Accessories

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


ส่งค่ากลับ

เป็นไบนารีที่ระบุว่ามีการโหลดรูปลักษณ์ของตัวละครผู้เล่นหรือไม่

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

This example prints the result of Player:HasAppearanceLoaded() after a player joins the game until the player's appearance has loaded.

Check if a Player's Appearance Has Loaded

local Players = game:GetService("Players")
local function onPlayerAdded(player)
local loaded = player:HasAppearanceLoaded()
print(loaded)
while not loaded do
loaded = player:HasAppearanceLoaded()
print(loaded)
task.wait()
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

IsVerified

ส่งค่า boolean ที่บ่งบอกถึงสถานะการตรวจสอบของผู้เล่นเมื่อถูกต้อง ผู้เล่นจะได้รับการตรวจสอบการตรวจสอบรวมถึง แต่ไม่ จำกัด เฉพาะหมายเลขโทรศัพท์ที่ไม่ใช่ VOIP หรือการตรวจสอบรหัสรัฐบาล

เมื่อดำเนินการตาม IsVerified ให้ใช้ความระมัดระวังเพื่อให้แน่ใจว่าการดำเนินการไม่บล็อกผู้ใช้ที่ไม่ได้รับการยืนยันทั้งหมดโดยไม่ได้ตั้งใจ

โปรดทราบว่าวิธีนี้สามารถเรียกได้เฉพาะในเซิร์ฟเวอร์ด้านหลังเท่านั้นเรียกผลลัพธ์ด้านลูกค้าในข้อผิดพลาดนอกจากนี้วิธีนี้จะส่งคืน false เสมอใน Studio


ส่งค่ากลับ

เป็นไบนารีที่ระบุว่าผู้เล่นได้รับการตรวจสอบหรือไม่

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

The following example prints "true" if the player is verified.

Using IsVerified

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

Kick

()

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

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

เมื่อใช้วิธีนี้จาก LocalScript เฉพาะไคลเอนต์ของผู้ใช้ท้องถิ่นเท่านั้นที่สามารถถูกเตะได้

พารามิเตอร์

message: string

ข้อความที่จะแสดงให้ผู้ใช้เมื่อเตะ

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

ส่งค่ากลับ

()

Move

()

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

นี่มีประโยชน์เมื่อสคริปต์ NPC Humanoids ที่เคลื่อนที่ไปรอบๆ แผนที่ - แต่ไม่ได้รับการควบคุมโดยการป้อนของผู้เล่นจริง

โปรดทราบว่าอาร์กิวเมนต์ที่สองของฟังก์ชันบ่งบอกว่าควรเคลื่อนย้ายผู้เล่นเป็นค่าพิกัดโลก ( false ) หรือผู้เล่นของผู้เล่น ( true )

พารามิเตอร์

walkDirection: Vector3

ทิศทาง Vector3 ที่ผู้เล่นควรเคลื่อนที่

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

เป็นไบนารีที่ระบุว่าผู้เล่นควรเคลื่อนที่เมื่อเทียบกับกล้องของผู้เล่น

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

ส่งค่ากลับ

()

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

Demonstrates moving a player relative to their camera's position using Player:Move().

The script first waits for the player's Character and Humanoid to load, as both are required before calling Player:Move(). Otherwise a warning will display in the Output.

Moving the Player relative to their Camera

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
-- Wait for the player's character and humanoid, which must exist before calling :Move()
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
character:WaitForChild("Humanoid")
-- The player will move until they are 50 studs away from the camera's position at the time of running
localPlayer:Move(Vector3.new(0, 0, -50), true)

RemoveReplicationFocus

()

พารามิเตอร์

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

ส่งค่ากลับ

()

SetAccountAge

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

ฟังก์ชัน SetAccountAge ตั้ง Player.AccountAge ของผู้เล่นในวัน

ใช้เพื่อตั้งค่าคุณสมบัติ Player ที่อธิบายว่าบัญชีผู้เล่นถูกลงทะเบียนไปนานแค่ไหนในวัน

สิ่งนี้ไม่ตั้งอายุของผู้เล่นในบัญชี แต่อายุของบัญชีเองเมื่อเทียบกับเมื่อสร้างครั้งแรก

พารามิเตอร์

accountAge: number

อายุของบัญชีในวัน.

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

ส่งค่ากลับ

()

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

This example demonstrates how the Player:SetAccountAge() function would be used if it was accessible. It sets the local player's account age to 100 days.

Setting the Player's Account Age

local Players = game:GetService("Players")
local player = Players.LocalPlayer
player:SetAccountAge(100)

This code sample adds a mark to players showing about how old their account is. The mark uses a player's account age to determine if they are a New Player, Veteran Player or Regular Player.

Account Age Mark

local Players = game:GetService("Players")
local MAX_AGE_NEW_PLAYER = 7 -- one week
local MIN_AGE_VETERAN = 365 -- one year
-- This function marks a part with text using a BillboardGui
local function mark(part, text)
local bbgui = Instance.new("BillboardGui")
bbgui.AlwaysOnTop = true
bbgui.StudsOffsetWorldSpace = Vector3.new(0, 2, 0)
bbgui.Size = UDim2.new(0, 200, 0, 50)
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(1, 0, 1, 0) -- Fill parent
textLabel.Text = text
textLabel.TextColor3 = Color3.new(1, 1, 1)
textLabel.TextStrokeTransparency = 0
textLabel.BackgroundTransparency = 1
textLabel.Parent = bbgui
-- Add to part
bbgui.Parent = part
bbgui.Adornee = part
end
local function onPlayerSpawned(player, character)
local head = character:WaitForChild("Head")
if player.AccountAge >= MIN_AGE_VETERAN then
mark(head, "Veteran Player")
elseif player.AccountAge <= MAX_AGE_NEW_PLAYER then
mark(head, "New Player")
else
mark(head, "Regular Player")
end
end
local function onPlayerAdded(player)
-- Listen for this player spawning
if player.Character then
onPlayerSpawned(player, player.Character)
end
player.CharacterAdded:Connect(function()
onPlayerSpawned(player, player.Character)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

SetSuperSafeChat

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

วิธีนี้กำหนดว่าผู้เล่นจะเห็นการแชทที่กรองโดย TextService:FilterStringAsync() หรือไม่ แทนการแชทปกติ


local Players = game:GetService("Players")
local player = Players.LocalPlayer
player:SetSuperSafeChat(true)

ไม่ว่าผู้เล่นจะมีการกรองแชทเปิดใช้งานหรือไม่ก็ตาม การแชททั้งหมดควรถูกกรองโดย TextService เมื่อส่งไปยังผู้เล่นคนอื่นหรือบนหน้าจอของผู้เล่นเองTextService:FilterStringAsync() ส่งคืนวัตถุ TextFilterResult ที่สามารถกรองได้ต่างกันตามการใช้งานที่ตั้งใจไว้ของข้อความ

พารามิเตอร์

value: boolean

เป็นไบนารีที่บ่งบอกว่าผู้เล่นเห็นการแชทที่กรองหรือไม่

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

ส่งค่ากลับ

()

GetFriendsOnline

ผลตอบแทน

ฟังก์ชันนี้返回 dictionary ของเพื่อนออนไลน์ที่จํากัดโดยค่า maxFriends ค่า ฟังก์ชันใช้แคช 30 วินาที

ในอาร์เรย์ที่ส่งคืนบางฟิลด์มีอยู่เฉพาะสำหรับประเภทตำแหน่งบางอย่างตัวอย่างเช่น PlaceId จะไม่ปรากฏเมื่อ LocationType เป็น 0 (เว็บไซต์บนมือถือ)


<th>ประเภท</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>ไอดีผู้เยี่ยมชม</b></td>
<td>จํานวน</td>
<td>คลาส <code>Class.Player.UserId</code> ของเพื่อน</td>
</tr>
<tr>
<td><b>ชื่อผู้ใช้</b></td>
<td>สตริง</td>
<td>ชื่อผู้ใช้ของเพื่อน</td>
</tr>
<tr>
<td><b>ชื่อที่แสดง</b></td>
<td>สตริง</td>
<td>คลาส <code>Class.Player.DisplayName</code> ของเพื่อน</td>
</tr>
<tr>
<td><b>ออนไลน์ล่าสุด</b></td>
<td>สตริง</td>
<td>เมื่อเพื่อนออนไลน์ล่าสุด</td>
</tr>
<tr>
<td><b>ออนไลน์อยู่</b></td>
<td>เป็นไปได้</td>
<td>หากเพื่อนออนไลน์อยู่ในขณะนี้</td>
</tr>
<tr>
<td><b>สถานที่สุดท้าย</b></td>
<td>สตริง</td>
<td>ชื่อตำแหน่งปัจจุบันของเพื่อน</td>
</tr>
<tr>
<td><b>รหัสสถานที่</b></td>
<td>จํานวน</td>
<td>รหัสสถานที่ของสถานที่สุดท้ายของเพื่อน</td>
</tr>
<tr>
<td><b>รหัสเกม</b></td>
<td>สตริง</td>
<td>The <code>แบบจำลองข้อมูล/JobId</code> ของสถานที่สุดท้ายของเพื่อน</td>
</tr>
<tr>
<td><b>LocationType</b></td><td>จํานวน</td>
<td>
ประเภทสถานที่ของสถานที่สุดท้ายของเพื่อน: 0 เว็บไซต์มือถือ เกมในมือถือ เว็บไซต์ในเกม เว็บไซต์ในเกม เว็บไซต์ในเกมในมือถือ เว็บไซต์ในเกมในเกมในเว็บไซต์ในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกมในเกในเกมในเกมในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในเกในในเกในในเกในเกในเกในเกในเกในเกในเกในในเกในเกในเกในเกในเกในเกในในเกในเกในเกในในเกในในเกในเกในเกในเกในเกในเกในเกในเกในในในเกในเนในในในในในในในในเนในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในในใน
</td>
</tr>
</tbody>
ชื่อ

พารามิเตอร์

maxFriends: number

จํานวนเพื่อนออนไลน์สูงสุดที่จะกลับ

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

ส่งค่ากลับ

สารานุกรมของเพื่อนออนไลน์ (ดูตารางด้านบน)

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

This example demonstrates how to get a dictionary of a player's online friends. It returns the maximum number of friends specified by the argument, or 200 if an argument is not provided.

Get a List of Online Friends

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local success, result = pcall(player.GetFriendsOnline, player, 10)
if success then
for _, friend in pairs(result) do
print(friend.UserName)
end
else
warn("Failed to get online players: " .. result)
end

GetRankInGroup

ผลตอบแทน

ฟังก์ชัน GetRankInGroup Player จะคืนอันดับของผู้เล่นในกลุ่มเป็นจํานวนเต็มระหว่าง 0 และ 255 โดยที่ 0 เป็นสมาชิกและ 255 เป็นเจ้าของกลุ่ม

การใช้สิ่งนี้ใน Script จะไม่ทำให้คุณได้รับข้อมูลล่าสุด เมื่อเทียบกับ LocalScriptหากผู้เล่นออกจากกลุ่มในขณะที่พวกเขาอยู่ในเกม, GetRankInGroup จะคิดว่าพวกเขาอยู่ในกลุ่มนั้นจนกว่าพวกเขาจะออกอย่างไรก็ตาม สิ่งนี้ไม่เกิดขึ้นเมื่อใช้กับ LocalScript

เนื่องจากวิธีการเก็บผลลัพธ์ในแคชดังนั้นการโทรหลายครั้งของ GetRankInGroup ในผู้เล่นเดียวกันด้วยรหัสกลุ่มเดียวกันจะให้ผลลัพธ์เดียวกันเมื่อวิธีการถูกเรียกครั้งแรกด้วยรหัสกลุ่มที่กำหนดพฤติกรรมการแคชอยู่บนพื้นฐานของแต่ละคู่: เซิร์ฟเวอร์ไม่แชร์แคชเดียวกับไคลเอนต์

พารามิเตอร์

groupId: number

The groupId ของกลุ่มที่ระบุ

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

ส่งค่ากลับ

อันดับของผู้เล่นในกลุ่ม

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

The code below will check if a player that has entered the game has a rank equal to 255, in a group with an ID of 2. If they are, it will print "Player is the owner of the group, 'LOL'!", otherwise "Player is NOT the owner of the group, 'LOL'!" will be printed to the output.

How to Check a Player's Rank in a Group

local Players = game:GetService("Players")
local function onPlayerAdded(player)
if player:GetRankInGroup(2) == 255 then
print("Player is the owner of the group, 'LOL'!")
else
print("Player is NOT the owner of the group, 'LOL'!")
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

GetRoleInGroup

ผลตอบแทน

ฟังก์ชัน GetRoleInGroup Player จะคืนบทบาทของผู้เล่นในกลุ่มเป็นข้อความหรือ แขก หากผู้เล่นไม่ได้เป็นส่วนหนึ่งของกลุ่ม

การใช้สิ่งนี้ใน Script จะไม่ทำให้คุณได้รับข้อมูลล่าสุด เมื่อเทียบกับ LocalScriptหากผู้เล่นออกจากกลุ่มในขณะที่พวกเขาอยู่ในเกม, GetRoleInGroup จะยังคิดว่าพวกเขาอยู่ในกลุ่มนั้นจนกว่าพวกเขาจะออกอย่างไรก็ตาม สิ่งนี้ไม่เกิดขึ้นเมื่อใช้กับ LocalScript

เนื่องจากวิธีการเก็บผลลัพธ์ในแคชดังนั้นการโทรหลายครั้งของ GetRoleInGroup ในผู้เล่นเดียวกันด้วยรหัสกลุ่มเดียวกันจะให้ผลลัพธ์เดียวกันเมื่อวิธีการถูกเรียกครั้งแรกด้วยรหัสกลุ่มที่กำหนดพฤติกรรมการแคชอยู่บนพื้นฐานของแต่ละคู่: เซิร์ฟเวอร์ไม่แชร์แคชเดียวกับไคลเอนต์

พารามิเตอร์

groupId: number

กลุ่ม groupId ของกลุ่มที่ระบุ

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

ส่งค่ากลับ

บทบาทของผู้เล่นในกลุ่มที่กำหนด หรือ แขก หากผู้เล่นไม่ใช่สมาชิก

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

The code below will print the name of the rank that the player is currently a part of, in a specific group. In this instance we're checking what rank the player is within a group which has a group ID of 2.

How to Check a Player's Role in a Group

local Players = game:GetService("Players")
local function onPlayerAdded(player)
print("Player is ranked as '", player:GetRoleInGroup(2), "' in group, 'LOL'!")
end
Players.PlayerAdded:Connect(onPlayerAdded)

IsFriendsWith

ผลตอบแทน

ฟังก์ชันนี้ส่งคำขอไปยังเว็บไซต์ Roblox ถามว่าผู้เล่นเป็นเพื่อนของผู้ใช้รายอื่นหรือไม่ โดยใช้ Player.UserId ของผู้ใช้นั้นฟังก์ชันนี้แคชผลลัพธ์ดังนั้นการโทรหลายครั้งของฟังก์ชันบนผู้เล่นเดียวกันด้วยผลลัพธ์เดียวกัน Player.UserId ผลการค้นหาLocalScript

พารามิเตอร์

userId: number

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

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

ส่งค่ากลับ

เป็นไบนารีที่ระบุว่าผู้เล่นเป็นเพื่อนของผู้ใช้ที่ระบุหรือไม่

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

The below example would print whether or not a recently added player is friends with Gordonrox24.

How to Check if a Player is a Friend

local Players = game:GetService("Players")
local function onPlayerAdded(player)
if player:IsFriendsWith(146569) then
print(player.Name .. " is friends with gordonrox24!")
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

IsInGroup

ผลตอบแทน

ฟังก์ชัน IsInGroup Player ส่งคำขอไปยังเว็บไซต์ Roblox ถามว่าผู้เล่นเป็นสมาชิกของกลุ่มหรือไม่ โดยใช้รหัสกลุ่มนั้น

การใช้สิ่งนี้ใน Script จะไม่ทำให้คุณได้รับข้อมูลล่าสุด เมื่อเทียบกับ LocalScriptหากผู้เล่นออกจากกลุ่มในขณะที่พวกเขาอยู่ในเกม IsInGroup จะคิดว่าพวกเขาอยู่ในกลุ่มนั้นจนกว่าพวกเขาจะออกอย่างไรก็ตาม สิ่งนี้ไม่เกิดขึ้นเมื่อใช้กับ LocalScript

เนื่องจากวิธีการเก็บผลลัพธ์ในคลัง ดังนั้นการโทรหลายครั้งของ IsInGroup ในผู้เล่นเดียวกันด้วยรหัสกลุ่มเดียวกันจะให้ผลลัพธ์เดียวกันเมื่อวิธีการถูกเรียกครั้งแรกด้วยรหัสกลุ่มที่กำหนดพฤติกรรมการแคชอยู่บนพื้นฐานของแต่ละคู่: เซิร์ฟเวอร์ไม่แชร์แคชเดียวกับไคลเอนต์

พารามิเตอร์

groupId: number

กลุ่ม groupId ของกลุ่มที่ระบุ

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

ส่งค่ากลับ

เป็นไบนารีที่ระบุว่าผู้เล่นอยู่ในกลุ่มที่ระบุหรือไม่

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

The below example will print "Player is in the Roblox Fan club!" if the newly added player is in the group with a groupId of 7.

How to Check if a Player is in a Group

local Players = game:GetService("Players")
local function onPlayerAdded(player)
if player:IsInGroup(7) then
print("Player is in the Roblox Fan club!")
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

LoadCharacter

()
ผลตอบแทน

ฟังก์ชัน LoadCharacter Player สร้างตัวละครใหม่สำหรับผู้เล่นโดยลบตัวเก่านอกจากนี้ยังล้าง Backpack และ PlayerGui ของผู้เล่น

สิ่งนี้มีประโยชน์ในกรณีที่คุณต้องการรีโหลดตัวละครโดยไม่ฆ่าผู้เล่น เช่น เมื่อคุณต้องการโหลดรูปลักษณ์ตัวละครใหม่หลังจากเปลี่ยนรูปลักษณ์ผู้เล่น Player.CharacterAppearance

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

หลังจากเรียก LoadCharacter สําหรับผู้เล่นคนหนึ่งแล้วไม่แนะนําให้เรียกอีกครั้งสําหรับผู้เล่นคนเดียวจนกว่าอีเวนต์ Player.CharacterAppearanceLoaded ของผู้เล่นคนนั้นจะถูกยิง

คำสั่งการโหลดตัวละคร

การเรียก Player:LoadCharacter() ด้วยอวตาร R15 จะสร้างเหตุการณ์ในลำดับต่อไปนี้ (หมายเหตุ: การสั่งซื้อ R6 แตกต่างออกไป):

  1. ชุดตัวละครผู้เล่น
  2. ผู้เล่น.CharacterAdded ไฟ
  3. ผู้เล่น .Changed ไฟด้วยค่า "ตัวละคร"
  4. การปรากฏตัวของตัวละครเริ่มต้น
  5. ผู้เล่น.CharacterAppearanceLoaded ไฟ
  6. ตัวละคร.Parent ตั้งค่าไปยัง DataModel
  7. การสร้างตัวละครและการขยายตัวละคร
  8. ตำแหน่งของการเกิดใหม่
  9. การคืนตัวละครโหลด

ส่งค่ากลับ

()

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

This script turns off auto-loading and simulates character respawning.

Turn Off Auto-Loading and Simulate Character Respawn

local Players = game:GetService("Players")
local RESPAWN_DELAY = 5
Players.CharacterAutoLoads = false
local function onPlayerAdded(player)
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local function onDied()
task.wait(RESPAWN_DELAY)
player:LoadCharacter()
end
humanoid.Died:Connect(onDied)
end
player.CharacterAdded:Connect(onCharacterAdded)
player:LoadCharacter()
end
Players.PlayerAdded:Connect(onPlayerAdded)

LoadCharacterWithHumanoidDescription

()
ผลตอบแทน

ฟังก์ชันนี้จะสร้างอวตารเพื่อให้มันมีทุกอย่างติดตั้งใน HumanoidDescription ที่ส่งมา

หลังจากเรียก LoadCharacterWithHumanoidDescription สำหรับผู้เล่นคนหนึ่งแล้วไม่แนะนำให้เรียกฟังก์ชันอีกครั้งสำหรับผู้เล่นคนเดียวจนกว่าจะเกิดเหตุการณ์ Player.CharacterAppearanceLoaded ของผู้เล่นคนนั้น

ดูเพิ่ม:

พารามิเตอร์

humanoidDescription: HumanoidDescription

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

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

ส่งค่ากลับ

()

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

To create a HumanoidDescription and then spawn a character with that description applied, add a Script (not a LocalScript) to the workspace and add this code to it.

Spawn Characters With HumanoidDescription

local Players = game:GetService("Players")
Players.CharacterAutoLoads = false
local function onPlayerAdded(player)
local humanoidDescription = Instance.new("HumanoidDescription")
humanoidDescription.HatAccessory = "2551510151,2535600138"
humanoidDescription.BodyTypeScale = 0.1
humanoidDescription.ClimbAnimation = 619521311
humanoidDescription.Face = 86487700
humanoidDescription.GraphicTShirt = 1711661
humanoidDescription.HeadColor = Color3.new(0, 1, 0)
player:LoadCharacterWithHumanoidDescription(humanoidDescription)
end
Players.PlayerAdded:Connect(onPlayerAdded)

RequestStreamAroundAsync

()
ผลตอบแทน

สำหรับประสบการณ์ที่ตัวอย่าง สตรีม เปิดใช้งาน คำขอที่เซิร์ฟเวอร์สตรีมไปยังภูมิภาคผู้เล่น (ส่วนและภูมิประเทศ) รอบตำแหน่งที่ระบุ X , Y , Z ในโลก 3Dมันมีประโยชน์ถ้าประสบการณ์รู้ว่า CFrame ของผู้เล่นจะถูกตั้งค่าเป็นตำแหน่งที่ระบุในอนาคตอันใกล้โดยไม่ให้ตำแหน่งด้วยการโทรนี้ ผู้เล่นอาจไม่ได้สตรีมในเนื้อหาสำหรับจุดหมาย ส่งผลให้เกิดการหยุดสตรีมหรือพฤติกรรมที่ไม่พึงประสงค์อื่นๆ

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

คำเตือนการใช้งาน

การร้องขอการสตรีมรอบพื้นที่ไม่ใช่การรับประกัน ว่าเนื้อหาจะปรากฏเมื่อคำขอสําเร็จ เนื่องจากการสตรีมได้รับผลกระทบจากความเร็วในการเชื่อมต่อเครือข่ายของไคลเอนต์ความจําจํากัดและปัจจัยอื่น ๆ

พารามิเตอร์

position: Vector3

ตำแหน่งทั่วโลกที่สตรีมร้องขอ

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

เวลาหมดอายุทางเลือกสำหรับคำขอ ระยะเวลาสูงสุดที่เครื่องพยายามสตรีมภูมิภาครอบพารามิเตอร์ position ก่อนที่จะทิ้งคำขอหากคุณไม่ระบุค่า เวลาที่หมดอายุจะไม่มีที่สิ้นสุดอย่างมีประสิทธิภาพอย่างไรก็ตาม หากไคลเอนต์มีหน่วยความจําน้อย เครื่องยนต์จะทิ้งคําขอสตรีมทั้งหมดแม้ว่าจะยังอยู่ภายในระยะเวลาหมดอายุก็ตาม

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

ส่งค่ากลับ

()

อีเวนต์

CharacterAdded

อีเวนต์ CharacterAdded จะเกิดขึ้นเมื่อตัวละครของผู้เล่นเกิดขึ้น (หรือเกิดใหม่)อีเวนต์นี้จะเกิดขึ้นเร็วหลังจากการตั้ง Player.Character เป็นค่าที่ไม่ใช่ nil หรือเรียก Player:LoadCharacter() ซึ่งเป็นก่อนที่ตัวละครจะถูกผูกกับ Workspace

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

โปรดทราบว่า Humanoid และส่วนตัวที่เป็นปกติของมัน (หัว ลําตัว และแขน) จะมีอยู่เมื่อเหตุการณ์นี้เกิดขึ้น แต่ไอเทมเสื้อผ้าเช่น Hats , Shirts และ Pants อาจใช้เวลาสองสามวินาทีในการเพิ่มตัวละครเชื่อมต่อ Instance.ChildAdded บนตัวละครที่เพิ่มเพื่อตรวจจับสิ่งเหล่านี้หรือรอให้เหตุการณ์ Player.CharacterAppearanceLoaded มั่นใจว่าตัวละครมีทุกอย่างพร้อม

พารามิเตอร์

character: Model

ตัวอย่างของตัวละครที่เกิดใหม่/เกิดใหม่


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

This code sample demonstrates the usage of Players.PlayerAdded, Player.CharacterAdded and Player.CharacterRemoving in order to detect the spawning and despawning of players' characters. You can use this as a boilerplate script to make changes to players' characters as they spawn, such as changing Humanoid.WalkSpeed.

Detecting Player Spawns and Despawns

local Players = game:GetService("Players")
local function onCharacterAdded(character)
print(character.Name .. " has spawned")
end
local function onCharacterRemoving(character)
print(character.Name .. " is despawning")
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
player.CharacterRemoving:Connect(onCharacterRemoving)
end
Players.PlayerAdded:Connect(onPlayerAdded)

This code sample will cause players to respawn at the same place they died. It does this by keeping track of where the player despawned using Player.CharacterRemoving. Note that the player's location is saved on-despawn, not on-death. This can be problematic if the player falls off a ledge and dies due to Workspace.FallenPartsDestroyHeight - their respawn position won't be saved in this case.

It's also important to note the need to "forget" the location of players who leave the game. We use Instance.ChildRemoved on Players instead of Players.PlayerRemoving. This is because PlayerRemoving fires before CharacterRemoving - and we need to make sure we don't forget the player's respawn location then immediately remember a new one (this is a memory leak; potentially many players could visit, respawn and leave). So, we use ChildRemoved on Players so the event fires after the character is removed.

Respawn at Despawn Location

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- This table maps "Player" objects to Vector3
local respawnLocations = {}
local function onCharacterAdded(character)
local player = Players:GetPlayerFromCharacter(character)
-- Check if we saved a respawn location for this player
if respawnLocations[player] then
-- Teleport the player there when their HumanoidRootPart is available
local hrp = character:WaitForChild("HumanoidRootPart")
-- Wait a brief moment before teleporting, as Roblox will teleport the
-- player to their designated SpawnLocation (which we will override)
RunService.Stepped:wait()
hrp.CFrame = CFrame.new(respawnLocations[player] + Vector3.new(0, 3.5, 0))
end
end
local function onCharacterRemoving(character)
-- Get the player and their HumanoidRootPart and save their death location
local player = Players:GetPlayerFromCharacter(character)
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp then
respawnLocations[player] = hrp.Position
end
end
local function onPlayerAdded(player)
-- Listen for spawns/despawns
player.CharacterAdded:Connect(onCharacterAdded)
player.CharacterRemoving:Connect(onCharacterRemoving)
end
local function onPlayerRemoved(player)
-- Forget the respawn location of any player who is leaving; this prevents
-- a memory leak if potentially many players visit
respawnLocations[player] = nil
end
-- Note that we're NOT using PlayerRemoving here, since CharacterRemoving fires
-- AFTER PlayerRemoving, we don't want to forget the respawn location then instantly
-- save another right after
Players.PlayerAdded:Connect(onPlayerAdded)
Players.ChildRemoved:Connect(onPlayerRemoved)

This code sample automatically removes Accessory objects like hats from the Player's character when they respawn. Warning: this includes hair, so this script may cause acute baldness.

When the Character() is added, we wait for RunService.Stepped to fire once (using the wait function of events). This is so the accessory removal logic runs one frame after the character spawns. A warning can appear if you delete accessories too quickly after the player spawns, so waiting one frame will avoid that.

Accessory Remover

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local function destroyAccessory(object)
if object:IsA("Hat") or object:IsA("Accessory") then
object:Destroy()
end
end
local function onCharacterAdded(character)
-- Wait a brief moment before removing accessories to avoid the
-- "Something unexpectedly set ___ parent to NULL" warning
RunService.Stepped:Wait()
-- Check for any existing accessories in the player's character
for _, child in pairs(character:GetChildren()) do
destroyAccessory(child)
end
-- Hats may be added to the character a moment after
-- CharacterAdded fires, so we listen for those using ChildAdded
character.ChildAdded:Connect(destroyAccessory)
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)

CharacterAppearanceLoaded

อีเวนต์นี้จะเกิดขึ้นเมื่อมีการสอดแทรกรูปลักษณ์เต็มของ Player.Character แล้ว

A Player.Character โดยทั่วไปมีช่วงของวัตถุที่ปรับเปลี่ยนรูปลักษณ์ของมันรวมถึง Accoutrements , Shirts , Pants และ CharacterMeshes .อีเวนต์นี้จะเกิดขึ้นเมื่อวัตถุทั้งหมดเหล่านี้ถูกสอดเข้าไปใน Player.Character

อีเวนต์นี้จะเกิดขึ้นเฉพาะในเซิร์ฟเวอร์เท่านั้น

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

พารามิเตอร์

character: Model

The Class.Player.Character``Class.Model .


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

This code sample will wait for accessories to fully load, print out how many there are, and then destroy them all.

Remove Accessories After Loading

local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- All accessories have loaded at this point
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- Make sure we disconnect our connection to the player after they leave
-- to allow the player to get garbage collected
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)

CharacterRemoving

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

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


local Players = game:GetService("Players")
local function onCharacterSpawned(player)
print(player.Name .. " is spawning")
end
local function onCharacterDespawned(player)
print(player.Name .. " is despawning")
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function()
onCharacterSpawned(player)
end)
player.CharacterRemoving:Connect(function()
onCharacterDespawned(player)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

อีเวนต์นี้เกี่ยวข้องกับ Character ของ Player เท่านั้นหากคุณต้องการติดตามเมื่อผู้เล่นเข้าร่วม/ออกจากเกมแทน ให้ใช้เหตุการณ์ Players.PlayerAdded และ Players.PlayerRemoving

พารามิเตอร์

character: Model

ตัวอย่างของตัวละครที่ถูกลบออก


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

This example prints the name of the character being removed, followed by "has died".

For instance, if Shedletsky's character was to die in-game, "Shedletsky has died." would be printed.

Player.CharacterRemoving

game.Players.PlayerAdded:Connect(function(player)
player.CharacterRemoving:Connect(function(character)
print(character.Name .. " has died.")
end)
end)

Chatted

อีเวนต์แชทจะเกิดขึ้นเมื่อ Player ประเภทข้อความพิมพ์และกด Enter ในแถบแชทที่ Roblox ให้ไว้สิ่งนี้ทำโดยใช้การผูก Luau บางอย่างโดยสคริปต์แชทเริ่มต้นคุณสามารถป้องกันผู้เล่นจากการแชทโดยใช้ StarterGui:SetCoreGuiEnabled() และปิดการใช้งานการแชท Enum.CoreGuiType

คําสั่งแชท

โดยใช้อีเวนต์นี้และฟังก์ชันการจัดการสตริงบางอย่างเช่น string.sub() และ string.lower() เป็นไปได้ที่จะสร้างคําสั่งแชทแม้กระทั่งด้วยอาร์กิวเมนต์เช่นชื่อผู้เล่นโดยปกติแล้วคําสั่งจะมีคํานําหน้าเช่น heal PlayerNameเพื่อตรวจสอบสัญลักษณ์ในสตริงใช้ string.sub() บนข้อความเพื่อตรวจสอบส่วนหนึ่งของข้อความ: string.sub(message, 1, 6) == "/heal " (โปรดทราบถึงการรวมช่องว่าง)จากนั้นแยกส่วนที่เหลือของคำสั่งโดยใช้ string.sub() อีกครั้ง: string.sub(message, 7) จะเท่ากับชื่อผู้เล่นตรวจสอบว่าผู้เล่นคนนั้นมีอยู่หรือไม่ และหากมี ให้ดำเนินการตามคำสั่ง (ในตัวอย่างนี้คือรักษาพวกเขา)ตรวจสอบตัวอย่างโค้ดสําหรับตัวอย่างคําสั่งแชท

การกรอง

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

พารามิเตอร์

message: string

เนื้อหาของข้อความที่ผู้เล่นพิมพ์ในแชท

recipient: Player

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


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

Setting chatted for all players. There is an easy way to make the Chatted event registered on all players. Simply use the Players.PlayerAdded event in combination with this event.

Player.Chatted

local Players = game:GetService("Players")
local function onPlayerAdded(player)
local function onChatted(message)
-- do stuff with message and player
print(message)
end
player.Chatted:Connect(onChatted)
end
Players.PlayerAdded:Connect(onPlayerAdded)

This code sample, although lengthy, is quite simple: detect when a player chats /play, then put them on the "Playing" team. When they die, move them back to the "Spectating" team.

Playing/Spectating Teams

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local teamPlaying = Teams.Playing
local teamSpectators = Teams.Spectating
local playCommand = "/play"
local function play(player)
player.Team = teamPlaying
player.TeamColor = teamPlaying.TeamColor
-- Respawn the player (moves them to spawn location)
player:LoadCharacter()
end
local function onPlayerDied(player, _character)
-- When someone dies, put them on the spectator team
player.Team = teamSpectators
end
local function onPlayerSpawned(player, character)
local human = character:WaitForChild("Humanoid")
human.Died:Connect(function()
onPlayerDied(player, character)
end)
end
local function onPlayerChatted(player, message)
if message:sub(1, playCommand:len()):lower() == playCommand then
play(player)
end
end
local function onPlayerAdded(player)
if player.Character then
onPlayerSpawned(player, player.Character)
end
player.CharacterAdded:Connect(function()
onPlayerSpawned(player, player.Character)
end)
player.Chatted:Connect(function(message, _recipient)
onPlayerChatted(player, message)
end)
end
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

This code sample allows any player to chat "/jointeam " where is the name of a team. It uses string manipulation using string.sub and string.lower to make the command case-insensitive and allow for partial matches. For example, "/jointeam red" will match the team "Red Robins".

Join Team Command

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
-- Command to choose a team (note the trailing space)
local joinCommand = "/jointeam "
local function findTeamByName(name)
-- First, check for the exact name of a team
if Teams:FindFirstChild(name) then
return Teams[name]
end
-- Let's check for case-insensitive partial matches, like "red" for "Red Robins"
for _, team in pairs(Teams:GetChildren()) do
if team.Name:sub(1, name:len()):lower() == name:lower() then
return team
end
end
-- If we get to this point, no team matched the one we were looking for :(
end
local function onPlayerChatted(player, message, _recipient)
-- Note: string.sub(message, ...) is the same as message:sub(...)
if message:sub(1, joinCommand:len()):lower() == joinCommand:lower() then
-- Matched "/JOINTEAM xyz" to our join command prefix "/jointeam "
local teamName = message:sub(joinCommand:len() + 1) -- Cut out the "xyz" from "/jointeam xyz"
local team = findTeamByName(teamName)
if team then
-- Set the team!
player.Team = team
player.Neutral = false
else
-- Tell the player that team could not be found :(
player.Team = nil
player.Neutral = true
end
end
end
local function onPlayerAdded(player)
player.Chatted:Connect(function(...)
onPlayerChatted(player, ...)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Idled

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

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

Roblox จะตัดการเชื่อมต่อกับผู้เล่นที่ไม่ได้ใช้งานอย่างน้อย 20 นาทีโดยอัตโนมัติ ดังนั้นอีเวนต์นี้จึงมีประโยชน์ในการเตือนผู้เล่นว่าพวกเขาจะถูกตัดการเชื่อมต่อในไม่ช้า ตัดการเชื่อมต่อผู้เล่นก่อนที่จะเป็น 20 นาที หรือคุณลักษณะอื่นๆ ที่ไม่ได้อยู่บนแป้นพิมพ์ (AFK)

เกิดขึ้นลองเชื่อมโยงเหตุการณ์นี้กับเหตุการณ์ Players.PlayerRemoving

พารามิเตอร์

time: number

นิ่งเฉย


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

Prints how long a player has been idle for.

Player.Idled

local Players = game:GetService("Players")
local function onIdled(idleTime)
print(`Player has been idle for {idleTime} seconds`)
if idleTime > 900 then
-- warn player that they've been idle for 15 minutes
-- and will be disconnected in another 5
end
end
Players.LocalPlayer.Idled:Connect(onIdled)

OnTeleport

ยิงเมื่อสถานะการเทเลพอร์ตของผู้เล่นเปลี่ยนแปลง อีเวนต์นี้มีประโยชน์ในการตรวจสอบว่าการเทเลพอร์ตสําเร็จหรือไม่

TeleportState คืออะไร?

เมื่อมีคำขอเทเลพอร์ตโดยใช้ TeleportService จะมีชุดของขั้นตอนก่อนที่ Player จะถูกเทเลพอร์ตสเตจปัจจุบันถูกแทนที่โดยค่า Enum.TeleportState ที่ได้รับจาก OnTeleportดูด้านล่างสำหรับตัวอย่างที่เป็นประโยชน์ของสิ่งนี้

พารามิเตอร์

teleportState: Enum.TeleportState

ใหม่ Enum.TeleportState ของ Player .

placeId: number

ID ของสถานที่ที่ Player ถูกเทเลพอร์ตไป

spawnName: string

ชื่อของจุดเกิดที่จะเทเลพอร์ตไปยังถ้า TeleportService:TeleportToSpawnByName() ถูกใช้


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

This example prints which stage of a teleport a player is at, as well as printing if the teleport was a failure.

Player.OnTeleport

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local playerOnTeleport = player
player.OnTeleport:Connect(function(teleportState, _placeId, _spawnName)
if teleportState == Enum.TeleportState.Started then
print("Teleport started (" .. playerOnTeleport.Name .. ")")
elseif teleportState == Enum.TeleportState.WaitingForServer then
print("Teleport waiting for server (" .. playerOnTeleport.Name .. ")")
elseif teleportState == Enum.TeleportState.InProgress then
print("Teleport in progress (" .. playerOnTeleport.Name .. ")")
elseif teleportState == Enum.TeleportState.Failed then
print("Teleport failed! (" .. playerOnTeleport.Name .. ")")
end
end)
end)