概要
屬性
方法
活動
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
範例程式碼
第一人稱遊戲
-- 獲取 Players 服務
local Players = game:GetService("Players")
-- 獲取本地玩家
local player = Players.LocalPlayer
-- 將 CameraMode 設置為 LockFirstPerson
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
範例程式碼
玩家.派對識別碼
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local partyId = player.PartyId
if partyId ~= "" then
print("玩家在派對中,識別碼為: " .. partyId)
else
print("玩家不在派對中")
end
player:GetPropertyChangedSignal("PartyId"):Connect(function()
if player.PartyId ~= "" then
print("玩家加入了識別碼為: " .. 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
-- 在工作空間中搜尋重生點
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
範例程式碼
玩家.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()
--> 身體顏色從角色中移除
--> 襯衫圖形從角色中移除
--> 襯衫從角色中移除
--> 褲子從角色中移除
--> 角色模型從角色中移除
--> 帽子從角色中移除
--> 襯衫從 角色中移除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 = {
\t"twitter",
\t"youtube",
\t"\ discord",
}
local function onPlayerAdded(player)
\tlocal source = player:GetJoinData().LaunchData
\t-- 檢查提供的來源是否有效
\tif source and table.find(ALLOWED_SOURCES,\ source) then
\t\t-- 更新數據存儲以追蹤來源普及度
\t\tlocal success, result = pcall(analyticsStore.IncrementAsync, analyticsStore,\ source)
\t\tif success then
\t\t\tprint(player.Name, "從", source, "加入 - 總計:", result)
\t\telse
\t\t\twarn("記錄加入來源失敗: \ " .. result)
\t\tend
\tend
end
Players.PlayerAdded:Connect(onPlayerAdded)推薦網址產生器
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
IsInGroup
IsVerified
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個stud
-- 在執行時的距離
localPlayer:Move(Vector3.new(0, 0, -50), true)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)
-- 等待片刻再移除配飾,以避免
-- "Something unexpectedly set ___ parent to 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(
參數
範例程式碼
玩家.傳送
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)