概要
属性
方法
BindToClose(function: function):() |
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 |
代码示例
获取服务()
local Workspace = game:GetService("Workspace")
local Lighting = game:GetService("Lighting")
-- 修改这些服务属性的示例
Workspace.Gravity = 20
Lighting.ClockTime = 4API 参考
属性
CreatorId
代码示例
侦测游戏所有者加入游戏
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)CreatorType
代码示例
侦测游戏所有者加入游戏
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)GearGenreSetting
Genre
lighting
PlaceVersion
代码示例
服务器版本号 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.new(0, 150, 0, 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 = StarterGuiPrivateServerId
代码示例
检测私人服务器
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())PrivateServerOwnerId
代码示例
检测私人服务器
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())VIPServerId
VIPServerOwnerId
workspace
方法
BindToClose
参数
返回
()
代码示例
在关闭之前保存玩家数据
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()
-- 每当有新线程时计数加一,线程完成时计数减一。当达到 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
-- 线程完成,计数减一
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)GetJobsInfo
返回
代码示例
获取工作信息
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)
endGetMessage
GetObjects
GetRemoteBuildMode
IsGearTypeAllowed
IsLoaded
返回
代码示例
自定义加载屏幕
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.new(1, 0, 1, 0)
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()SavePlace
活动
AllowedGearTypeChanged
GraphicsQualityChangeRequest
参数
代码示例
处理用户的图形质量更改
game.GraphicsQualityChangeRequest:Connect(function(betterQuality)
if betterQuality then
print("用户请求提高图形质量!")
else
print("用户请求降低图形质量!")
end
end)ItemChanged
ServerRestartScheduled
DataModel.ServerRestartScheduled(
参数
代码示例
服务器重启已安排
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
end
end)回调
OnClose