Data Model
摘要
属性
方法
BindToClose(function: (Enum.CloseReason) -> ()):() |
GetObjects(url: ContentId):Instances |
IsGearTypeAllowed(gearType: Enum.GearType):boolean |
SavePlace(saveFilter: Enum.SaveFilter):boolean |
SetPlaceId(placeId: number):() |
SetUniverseId(universeId: number):() |
活动
GraphicsQualityChangeRequest(betterQuality: boolean):RBXScriptSignal |
ItemChanged(object: Instance,descriptor: string):RBXScriptSignal |
ServerRestartScheduled(restartTime: DateTime,source: Enum.CloseReason,attributes: Dictionary):RBXScriptSignal |
代码示例
GetService()
local Workspace = game:GetService("Workspace")
local Lighting = game:GetService("Lighting")
-- 修改这些服务属性的示例
Workspace.Gravity = 20
Lighting.ClockTime = 4API 参考
属性
Creator Id
代码示例
检测游戏所有者何时加入游戏
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
if game.CreatorType == Enum.CreatorType.User then
if player.UserId == game.CreatorId then
print("游戏所有者已加入游戏!")
end
elseif game.CreatorType == Enum.CreatorType.Group then
if player:IsInGroupAsync(game.CreatorId) then
print("拥有该地点的组的成员已加入游戏!")
end
end
end)Creator Type
代码示例
检测游戏所有者何时加入游戏
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
if game.CreatorType == Enum.CreatorType.User then
if player.UserId == game.CreatorId then
print("游戏所有者已加入游戏!")
end
elseif game.CreatorType == Enum.CreatorType.Group then
if player:IsInGroupAsync(game.CreatorId) then
print("拥有该地点的组的成员已加入游戏!")
end
end
end)Gear Genre Setting
Genre
lighting
Place Version
代码示例
服务器版本号 GUI
local StarterGui = game:GetService("StarterGui")
local versionGui = Instance.new("ScreenGui")
local textLabel = Instance.new("TextLabel")
textLabel.Position = UDim2.new(1, -10, 1, 0)
textLabel.AnchorPoint = Vector2.new(1, 1)
textLabel.Size = UDim2.fromOffset(150, 40)
textLabel.BackgroundTransparency = 1
textLabel.TextColor3 = Color3.new(1, 1, 1)
textLabel.TextStrokeTransparency = 0
textLabel.TextXAlignment = Enum.TextXAlignment.Right
textLabel.TextScaled = true
local placeVersion = game.PlaceVersion
textLabel.Text = string.format("服务器版本: %s", placeVersion)
textLabel.Parent = versionGui
versionGui.Parent = StarterGuiPrivate Server Id
代码示例
检测私人服务器
local function getServerType()
if game.PrivateServerId ~= "" then
if game.PrivateServerOwnerId ~= 0 then
return "VIPServer"
else
return "ReservedServer"
end
else
return "StandardServer"
end
end
print(getServerType())Private Server Owner Id
代码示例
检测私人服务器
local function getServerType()
if game.PrivateServerId ~= "" then
if game.PrivateServerOwnerId ~= 0 then
return "VIPServer"
else
return "ReservedServer"
end
else
return "StandardServer"
end
end
print(getServerType())VIP Server Id
VIP Server Owner Id
workspace
方法
Bind To Close
Data Model: Bind To Close(function:(Enum.CloseReason) -> ()):()
参数
function:(Enum.CloseReason) -> () |
返回
()
代码示例
在关闭之前保存玩家数据
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local allPlayerSessionDataCache = {}
local function savePlayerDataAsync(userId, data)
return playerDataStore:UpdateAsync(userId, function(oldData)
return data
end)
end
local function onServerShutdown(closeReason)
if RunService:IsStudio() then
-- 避免将工作室数据写入生产环境并阻止测试会话关闭
return
end
-- 参考用于稍后挂起和恢复
local mainThread = coroutine.running()
-- 每个新线程计数加1,线程完成时计数减1。当计数为0时,
-- 单个线程知道这是最后一个完成的线程,应该恢复主线程
local numThreadsRunning = 0
-- 稍后调用此函数会在新线程上启动,因为 coroutine.wrap
local startSaveThread = coroutine.wrap(function(userId, sessionData)
-- 执行保存操作
local success, result = pcall(savePlayerDataAsync, userId, sessionData)
if not success then
-- 可以实现重试
warn(string.format("保存 %d 的数据失败: %s", userId, result))
end
-- 线程完成,计数减1
numThreadsRunning -= 1
if numThreadsRunning == 0 then
-- 这是最后一个完成的线程,恢复主线程
coroutine.resume(mainThread)
end
end)
-- 这假设在 PlayerRemoving 时,playerData 会在最终保存时从数据表中清除,
-- 所以这在迭代所有仍在游戏中且尚未保存的数据的玩家数据
for userId, sessionData in pairs(allPlayerSessionDataCache) do
numThreadsRunning += 1
-- 此循环在任何保存线程开始之前完成运行并计数 numThreadsRunning,
-- 因为 coroutine.wrap 在开始时具有内置的延迟
startSaveThread(userId, sessionData)
end
if numThreadsRunning > 0 then
-- 在保存线程完成之前暂停关闭。由最后一个保存线程在完成时恢复
coroutine.yield()
end
end
game:BindToClose(onServerShutdown)绑定和处理游戏关闭
game:BindToClose(function(closeReason)
print(`关闭原因 {closeReason}`)
task.wait(3)
print("完成")
end)Get Jobs Info
返回
代码示例
获取职位信息
local jobInfo = game:GetJobsInfo()
local jobTitles = jobInfo[1]
table.remove(jobInfo, 1)
local divider = string.rep("-", 120)
print(divider)
warn("职位信息:")
print(divider)
for _, job in pairs(jobInfo) do
for jobIndex, jobValue in pairs(job) do
local jobTitle = jobTitles[jobIndex]
warn(jobTitle, "=", jobValue)
end
print(divider)
endGet Message
Get Objects
Get Remote Build Mode
Is Gear Type Allowed
Is Loaded
返回
代码示例
自定义加载屏幕
local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
-- 创建一个基本的加载屏幕
local screenGui = Instance.new("ScreenGui")
screenGui.IgnoreGuiInset = true
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.fromScale(1, 1)
textLabel.BackgroundColor3 = Color3.fromRGB(0, 20, 40)
textLabel.Font = Enum.Font.GothamMedium
textLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)
textLabel.Text = "加载中"
textLabel.TextSize = 28
textLabel.Parent = screenGui
-- 将整个屏幕 GUI 作为父级添加到玩家 GUI
screenGui.Parent = playerGui
-- 移除默认的加载屏幕
ReplicatedFirst:RemoveDefaultLoadingScreen()
--task.wait(3) -- 可选:强制屏幕至少显示几秒钟
if not game:IsLoaded() then
game.Loaded:Wait()
end
screenGui:Destroy()Save Place
活动
Allowed Gear Type Changed
Graphics Quality Change Request
参数
代码示例
处理用户在图形质量上的更改
game.GraphicsQualityChangeRequest:Connect(function(betterQuality)
if betterQuality then
print("用户请求提高图形质量!")
else
print("用户请求降低图形质量!")
end
end)Item Changed
Server Low Memory Warning
代码示例
服务器低内存警告
-- 游戏代码将非必要的外观部件在创建时父级到此文件夹
local debrisFolder = workspace:WaitForChild("Debris")
game.ServerLowMemoryWarning:Connect(function()
debrisFolder:ClearAllChildren()
print("服务器内存压力过大。已清除外观碎片。")
end)Server Restart Scheduled
Data Model. Server Restart Scheduled(
参数
代码示例
服务器重启已安排
local roundInProgress = false -- 在游戏代码的其他地方设置,当游戏回合开始或结束时
local restartPending = false
game.ServerRestartScheduled:Connect(function(restartTime, source, attributes)
local msg = attributes and attributes.message or ""
local timeStr = restartTime:FormatLocalTime("LTS", "en-us") -- 由于这是在服务器上运行,它将是 UTC
-- 通过 remote:FireAllClients() 将这些字段转发给客户端,以在您的 UI 中显示自定义消息
print(string.format("服务器将在 %s 重新启动,来源: %s. 原因: %s.", timeStr, tostring(source), msg))
if not roundInProgress then
print("没有回合进行中。将在 10 秒后传送玩家")
task.wait(10)
-- 在计划重新启动时传送玩家将把他们带到最新场所版本的服务器
game:GetService("TeleportService"):TeleportAsync(game.PlaceId, game:GetService("Players"):GetPlayers())
else
-- 如果 restartPending 为 true,您可以添加代码在游戏回合结束后传送玩家。
restartPending = true
print("重启待定:", restartPending)
end
end)回调
On Close