概要
属性
方法
活动
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
代码示例
玩家.派对ID
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
-- 在工作区中查找生成位置
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 = {
"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)推荐链接生成器
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 个单位
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)
-- 等待片刻再移除配件,以避免
-- "某个东西意外地将 ___ 父级设置为 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)