概要
屬性
方法
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" -- 返回 "VIP伺服器"
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" -- 返回 "VIP伺服器"
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)
-- 假設 playerData 在 PlayerRemoving 時的最後儲存後從數據表中清除,
-- 因此,此處遍歷所有仍在遊戲中且尚未儲存的玩家數據
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(`Closing with reason {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