概要
屬性
方法
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 |
範例程式碼
GetService()
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 "VIP伺服器"
else
return "保留伺服器"
end
else
return "標準伺服器"
end
end
print(getServerType())PrivateServerOwnerId
範例程式碼
檢測私人伺服器
local function getServerType()
if game.PrivateServerId ~= "" then
if game.PrivateServerOwnerId ~= 0 then
return "VIP伺服器"
else
return "保留伺服器"
end
else
return "標準伺服器"
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