Player
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ข้อมูลสรุป
คุณสมบัติ
วิธีการ
เหตุการณ์
CharacterAdded(character: Model):RBXScriptSignal |
CharacterAppearanceLoaded(character: Model):RBXScriptSignal |
CharacterRemoving(character: Model):RBXScriptSignal |
Chatted(message: string,recipient: Player):RBXScriptSignal |
Idled(time: number):RBXScriptSignal |
OnTeleport(teleportState: Enum.TeleportState,placeId: number,spawnName: string):RBXScriptSignal |
เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
Auto Jump Enabled
ตัวอย่างโค้ด
สลับการกระโดดอัตโนมัติ
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent
local function update()
-- อัปเดตข้อความปุ่ม
if player.AutoJumpEnabled then
button.Text = "การกระโดดอัตโนมัติเปิดอยู่"
else
button.Text = "การกระโดดอัตโนมัติปิดอยู่"
end
-- สะท้อนคุณสมบัติในตัวละครของผู้เล่น หากพวกเขามี
if player.Character then
local human = player.Character:FindFirstChild("Humanoid")
if human then
human.AutoJumpEnabled = player.AutoJumpEnabled
end
end
end
local function onActivated()
-- สลับการกระโดดอัตโนมัติ
player.AutoJumpEnabled = not player.AutoJumpEnabled
-- อัปเดตทุกอย่างอื่น
update()
end
button.Activated:Connect(onActivated)
update()Camera Mode
ตัวอย่างโค้ด
เล่นในมุมมองบุคคลที่หนึ่ง
local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.CameraMode = Enum.CameraMode.LockFirstPersonCharacter
ตัวอย่างโค้ด
ตัวละคร
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()
endCharacter Appearance
Data Complexity
Data Ready
Follow User Id
ตัวอย่างโค้ด
การแจ้งเตือนผู้ติดตาม
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 = "คุณถูกติดตามเข้าสู่เกมนี้โดย " .. newPlayer.Name .. "!"
task.delay(3, function()
if textLabel then
textLabel:Destroy()
end
end)
end
end
Players.PlayerAdded:Connect(onPlayerAdded)Has Roblox Subscription
ตัวอย่างโค้ด
ตรวจสอบสถานะการสมัครสมาชิก Roblox
local Players = game:GetService("Players")
local player = Players.LocalPlayer
if player.HasRobloxSubscription then
-- มอบเนื้อหาหรือสิทธิพิเศษเฉพาะสมาชิก
endMembership Type
Party Id
ตัวอย่างโค้ด
Player.PartyId
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local partyId = player.PartyId
if partyId ~= "" then
print("ผู้เล่นอยู่ในปาร์ตี้ที่มี ID: " .. partyId)
else
print("ผู้เล่นไม่ได้อยู่ในปาร์ตี้")
end
player:GetPropertyChangedSignal("PartyId"):Connect(function()
if player.PartyId ~= "" then
print("ผู้เล่นเข้าร่วมปาร์ตี้ที่มี ID: " .. player.PartyId)
else
print("ผู้เล่นออกจากปาร์ตี้")
end
end)
end)Respawn Location
ตัวอย่างโค้ด
เปลี่ยนจุดเกิดเมื่อสัมผัส
local Players = game:GetService("Players")
local function addSpawn(spawnLocation)
-- ฟังการสัมผัสจุดเกิด
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")
-- ตรวจสอบให้แน่ใจว่าตัวละครยังไม่ตาย
if humanoid and humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
print("ตั้งค่าจุดเกิด")
player.RespawnLocation = spawnLocation
end
end
end
end)
end
local firstSpawn
-- มองหาจุดเกิดใน workspace
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
-- ฟังผู้เล่นใหม่
Players.PlayerAdded:Connect(playerAdded)
-- ผ่านผู้เล่นที่มีอยู่
for _, player in pairs(Players:GetPlayers()) do
playerAdded(player)
endThird Party Text Chat Restriction Status
User
ตัวอย่างโค้ด
ผู้ใช้
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local user = player.User
print("รหัสผู้ใช้โดเมน:", user.Id)
print("ประเภทโดเมน:", user.DomainType)
print("รหัสโดเมน:", user.DomainId)
end)User Id
ตัวอย่างโค้ด
Player.UserId
local Players = game:GetService("Players")
local function onPlayerAdded(player)
print(player.UserId)
end
Players.PlayerAdded:Connect(onPlayerAdded)ข้อมูลจัดเก็บไปยังกระดานผู้นำ
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
-- ไม่สามารถดึงข้อมูลได้
warn(result)
end
leaderstats.Parent = player
end
Players.PlayerAdded:Connect(onPlayerAdded)user Id
วิธีการ
Clear Cached Avatar Appearance
Player: Clear Cached Avatar Appearance():()
ส่งค่ากลับ
()
Clear Character Appearance
Player: Clear Character 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, "ถูกลบออกจากตัวละคร")
end
character.ChildRemoved:Connect(onChildRemoved)
player:ClearCharacterAppearance()
--> BodyColors ถูกลบออกจากตัวละคร
--> ShirtGraphic ถูกลบออกจากตัวละคร
--> Shirt ถูกลบออกจากตัวละคร
--> Pants ถูกลบออกจากตัวละคร
--> CharacterMesh ถูกลบออกจากตัวละคร
--> Hat ถูกลบออกจากตัวละคร
--> Shirt ถูกลบออกจากตัวละครDistance From Character
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
การวัดระยะห่างระหว่างผู้เล่นกับตำแหน่ง
local Players = game:GetService("Players")
for _, player in pairs(Players:GetPlayers()) do
print(player:DistanceFromCharacter(Vector3.new(0, 0, 0)))
endระยะห่างจากตัวละคร
local position1 = Vector3.new(0, 0, 0)
local position2 = Vector3.new(0, 10, 0)
local distance = (position1 - position2).Magnitude
print(distance) --> 10Get Camera State
Player: Get Camera State():CameraState
ส่งค่ากลับ
CameraState
ตัวอย่างโค้ด
รับสถานะกล้อง
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local cameraState = player:GetCameraState()
print(cameraState.CFrame)
print(cameraState.FieldOfView)
print(cameraState.ViewportSize)Get Friends Online
Get Friends Online Async
พารามิเตอร์
| ค่าเริ่มต้น: 200 |
ส่งค่ากลับ
ตัวอย่างโค้ด
รับรายชื่อการเชื่อมต่อออนไลน์
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local success, result = pcall(player.GetFriendsOnlineAsync, player, 10)
if success then
for _, friend in pairs(result) do
print(friend.UserName)
end
else
warn("ไม่สามารถรับผู้เล่นออนไลน์ได้: " .. result)
endGet Join Data
ส่งค่ากลับ
ตัวอย่างโค้ด
ติดตามแหล่งที่มาของการเข้าชม
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
-- ตรวจสอบว่าแหล่งที่มาที่ให้มาถูกต้องหรือไม่
if source and table.find(ALLOWED_SOURCES, source) then
-- อัปเดตข้อมูลในร้านค้าเพื่อติดตามความนิยมของแหล่งที่มา
local success, result = pcall(analyticsStore.IncrementAsync, analyticsStore, source)
if success then
print(player.Name, "เข้าร่วมจาก", source, "- รวม:", result)
else
warn("ไม่สามารถบันทึกแหล่งที่มาของการเข้าร่วม: " .. result)
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)เครื่องมือสร้าง URL แนะนำ
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 -- หลีกเลี่ยงการอัปเดตคุณสมบัติซ้ำ
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)การใช้ตารางเป็นข้อมูลการเริ่มต้น
local HttpService = game:GetService("HttpService")
local DATA_CHARACTER_LIMIT = 200
local function encodeTableAsLaunchData(data)
-- แปลงตารางเป็นสตริง
local jsonEncodedData = HttpService:JSONEncode(data)
if #jsonEncodedData <= DATA_CHARACTER_LIMIT then
-- หนีอักขระที่อาจไม่ถูกต้อง เช่น ช่องว่าง
local urlEncodedData = HttpService:UrlEncode(jsonEncodedData)
return true, urlEncodedData
else
-- รายงานข้อผิดพลาดเกี่ยวกับขีดจำกัดอักขระ
return false, ("ตารางที่เข้ารหัสเกินขีดจำกัดอักขระ %d"):format(DATA_CHARACTER_LIMIT)
end
end
local sampleData = {
joinMessage = "สวัสดี!",
urlCreationDate = os.time(),
magicNumbers = {
534,
1337,
746733573,
},
}
local success, encodedData = encodeTableAsLaunchData(sampleData)
if success then
print(encodedData)
else
warn("ล้มเหลวในการเข้ารหัสข้อมูลการเริ่มต้น: " .. encodedData)
endการถอดรหัสข้อมูลการเปิดตัว JSON
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local function onPlayerAdded(player)
local launchData = player:GetJoinData().LaunchData
if launchData then
-- พยายามถอดรหัสข้อมูล
local success, result = pcall(HttpService.JSONDecode, HttpService, launchData)
if success then
print(player.Name, "เข้าร่วมด้วยข้อมูล:", result)
else
-- นี่อาจเกิดจากผู้ใช้ทำให้ URL เสียหาย
warn("ไม่สามารถถอดรหัสข้อมูลการเปิดตัวได้:" .. result)
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)ตัวอย่าง TeleportData ของเซิร์ฟเวอร์
local Players = game:GetService("Players")
local approvedPlaceIds = { 1 } -- ใส่ PlaceIds ที่ได้รับการอนุมัติที่นี่
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()
-- ตรวจสอบว่าข้อมูลนี้ถูกส่งโดยสถานที่ที่ได้รับการอนุมัติ
if isPlaceIdApproved(joinData.SourcePlaceId) then
local teleportData = joinData.TeleportData
if teleportData then
local currentLevel = teleportData.currentLevel
print(player.Name .. " อยู่ที่ระดับ " .. currentLevel)
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)Get Rank In Group
Get Rank In Group Async
Get Role In Group
Get Role In Group Async
Has Appearance 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)Is Best Friends With
Is Friends With
is Friends With
Is Friends With Async
Is In Group
Is In Group Async
Is Verified
พารามิเตอร์
| ค่าเริ่มต้น: Low |
ส่งค่ากลับ
ตัวอย่างโค้ด
การใช้ 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)Load Boolean
load Boolean
Load Character
Load Character Appearance
Load Character Async
Player: Load Character Async():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปิดการโหลดอัตโนมัติและจำลองการเกิดใหม่ของตัวละคร
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:LoadCharacterAsync()
end
humanoid.Died:Connect(onDied)
end
player.CharacterAdded:Connect(onCharacterAdded)
player:LoadCharacterAsync()
end
Players.PlayerAdded:Connect(onPlayerAdded)Load Character With Avatar Rules Async
พารามิเตอร์
ส่งค่ากลับ
()
Load Character With Humanoid Description
Load Character With Humanoid Description Async
Player: Load Character With Humanoid Description Async(
):()
พารามิเตอร์
| ค่าเริ่มต้น: Default |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
สร้างตัวละครด้วย 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:LoadCharacterWithHumanoidDescriptionAsync(humanoidDescription)
end
Players.PlayerAdded:Connect(onPlayerAdded)Load Instance
load Instance
Load Number
load Number
Load String
load String
Move
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การเคลื่อนที่ของผู้เล่นสัมพันธ์กับกล้องของพวกเขา
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
-- รอให้ตัวละครและฮิวมานอยด์ของผู้เล่นโหลด ซึ่งต้องมีอยู่ก่อนที่จะเรียกใช้ :Move()
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
character:WaitForChild("Humanoid")
-- ผู้เล่นจะเคลื่อนที่จนกว่าพวกเขาจะอยู่ห่างจากตำแหน่งของกล้อง 50 สตัดในขณะเรียกใช้
localPlayer:Move(Vector3.new(0, 0, -50), true)Prompt Age Check
Player: Prompt Age Check():()
ส่งค่ากลับ
()
Remove Replication Focus
Request Stream Around Async
Save Boolean
save Boolean
Save Instance
save Instance
Save Number
save Number
Save String
save String
Wait For Data Ready
wait For Data Ready
เหตุการณ์
Character Added
พารามิเตอร์
ตัวอย่างโค้ด
การตรวจจับการเกิดและการหายไปของผู้เล่น
local Players = game:GetService("Players")
local function onCharacterAdded(character)
print(character.Name .. " ได้เกิดขึ้น")
end
local function onCharacterRemoving(character)
print(character.Name .. " กำลังหายไป")
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
player.CharacterRemoving:Connect(onCharacterRemoving)
end
Players.PlayerAdded:Connect(onPlayerAdded)เครื่องลบอุปกรณ์เสริม
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)
-- รอช่วงเวลาสั้น ๆ ก่อนที่จะลบอุปกรณ์เสริมเพื่อหลีกเลี่ยง
-- คำเตือน "บางอย่างตั้งค่า ___ parent เป็น NULL โดยไม่คาดคิด"
RunService.Stepped:Wait()
-- ตรวจสอบอุปกรณ์เสริมที่มีอยู่ในตัวละครของผู้เล่น
for _, child in pairs(character:GetChildren()) do
destroyAccessory(child)
end
-- หมวกอาจถูกเพิ่มเข้ามาในตัวละครหลังจาก
-- CharacterAdded ถูกเรียก ดังนั้นเราจึงฟังสำหรับสิ่งเหล่านั้นโดยใช้ ChildAdded
character.ChildAdded:Connect(destroyAccessory)
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)Character Appearance Loaded
พารามิเตอร์
ตัวอย่างโค้ด
ลบอุปกรณ์เสริมหลังจากโหลดเสร็จ
local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- อุปกรณ์เสริมทั้งหมดได้โหลดแล้วในจุดนี้
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("กำลังทำลายอุปกรณ์เสริม %d ชิ้นสำหรับ %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- ตรวจสอบให้แน่ใจว่าเราได้ตัดการเชื่อมต่อกับผู้เล่นหลังจากที่พวกเขาออก
-- เพื่อให้ผู้เล่นสามารถถูกเก็บขยะได้
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)Character Removing
พารามิเตอร์
ตัวอย่างโค้ด
การตรวจจับการเกิดและการหายไปของผู้เล่น
local Players = game:GetService("Players")
local function onCharacterAdded(character)
print(character.Name .. " ได้เกิดขึ้น")
end
local function onCharacterRemoving(character)
print(character.Name .. " กำลังหายไป")
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
player.CharacterRemoving:Connect(onCharacterRemoving)
end
Players.PlayerAdded:Connect(onPlayerAdded)Chatted
On Teleport
Player. On Teleport(
พารามิเตอร์
ตัวอย่างโค้ด
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("การเทเลพอร์ตเริ่มต้นแล้ว (" .. playerOnTeleport.Name .. ")")
elseif teleportState == Enum.TeleportState.WaitingForServer then
print("การเทเลพอร์ตกำลังรอเซิร์ฟเวอร์ (" .. playerOnTeleport.Name .. ")")
elseif teleportState == Enum.TeleportState.InProgress then
print("การเทเลพอร์ตกำลังดำเนินการ (" .. playerOnTeleport.Name .. ")")
elseif teleportState == Enum.TeleportState.Failed then
print("การเทเลพอร์ตล้มเหลว! (" .. playerOnTeleport.Name .. ")")
end
end)
end)