Engine Class
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
คุณสมบัติ
AutoJumpEnabled
ตัวอย่างโค้ด
ปิด/เปิดการกระโดดโดยอัตโนมัติ
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()CameraMode
ตัวอย่างโค้ด
เล่นในมุมมองบุคคลที่หนึ่ง
local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.CameraMode = Enum.CameraMode.LockFirstPersonCharacterAppearance
DataComplexity
DataReady
FollowUserId
ตัวอย่างโค้ด
การแจ้งเตือนเมื่อมีคนติดตาม
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)HasRobloxSubscription
ตัวอย่างโค้ด
ตรวจสอบสถานะการสมัครสมาชิก Roblox
local Players = game:GetService("Players")
local player = Players.LocalPlayer
if player.HasRobloxSubscription then
-- มอบเนื้อหาหรือสิทธิพิเศษเฉพาะสมาชิก
endMembershipType
PartyId
ตัวอย่างโค้ด
ผู้เล่น.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)RespawnLocation
ตัวอย่างโค้ด
เปลี่ยนจุดเกิดเมื่อสัมผัส
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)
endThirdPartyTextChatRestrictionStatus
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)userId
วิธีการ
ClearCharacterAppearance
Player:ClearCharacterAppearance():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
วิธีการล้างรูปลักษณ์ของตัวละคร
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 ถูกลบออกจากตัวละคร
--> เสื้อถูกลบออกจากตัวละคร
--> กางเกงถูกลบออกจากตัวละคร
--> CharacterMesh ถูกลบออกจากตัวละคร
--> หมวกถูกลบออกจากตัวละคร
--> เสื้อถูกลบออกจากตัวละครClearCachedAvatarAppearance
Player:ClearCachedAvatarAppearance():()
ส่งค่ากลับ
()
DistanceFromCharacter
GetFriendsOnline
GetFriendsOnlineAsync
พารามิเตอร์
| ค่าเริ่มต้น: 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)
endGetJoinData
ส่งค่ากลับ
ตัวอย่างโค้ด
ติดตามแหล่งที่มาของการเข้าชม
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
-- อัปเดตข้อมูลใน store เพื่อติดตามความนิยมของแหล่งที่มานั้น
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)ตัวอย่างข้อมูลการโทรศัพท์ของเซิร์ฟเวอร์
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)GetRankInGroup
GetRankInGroupAsync
GetRoleInGroup
GetRoleInGroupAsync
HasAppearanceLoaded
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบว่ารูปลักษณ์ของผู้เล่นถูกโหลดหรือไม่
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)IsBestFriendsWith
IsFriendsWith
isFriendsWith
IsFriendsWithAsync
IsInGroup
IsInGroupAsync
IsVerified
ส่งค่ากลับ
ตัวอย่างโค้ด
การใช้ 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)LoadBoolean
loadBoolean
LoadCharacter
LoadCharacterAppearance
LoadCharacterAsync
Player:LoadCharacterAsync():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปิดการโหลดอัตโนมัติและจำลองการเกิดใหม่ของตัวละคร
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)LoadCharacterWithHumanoidDescription
LoadCharacterWithHumanoidDescriptionAsync
Player:LoadCharacterWithHumanoidDescriptionAsync(
):()
พารามิเตอร์
| ค่าเริ่มต้น: "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)LoadInstance
loadInstance
LoadNumber
loadNumber
LoadString
loadString
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)RemoveReplicationFocus
RequestStreamAroundAsync
SaveBoolean
saveBoolean
SaveInstance
saveInstance
SaveNumber
saveNumber
SaveString
saveString
WaitForDataReady
waitForDataReady
เหตุการณ์
CharacterAdded
พารามิเตอร์
ตัวอย่างโค้ด
การตรวจจับการเกิดและการหายไปของผู้เล่น
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)CharacterAppearanceLoaded
พารามิเตอร์
ตัวอย่างโค้ด
ลบอุปกรณ์หลังจากโหลดเสร็จแล้ว
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)CharacterRemoving
พารามิเตอร์
ตัวอย่างโค้ด
การตรวจจับการเกิดและการหายไปของผู้เล่น
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
OnTeleport
Player.OnTeleport(
พารามิเตอร์
ตัวอย่างโค้ด
ผู้เล่น.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)