數據模型(通常稱為之後使用來存取它的全球變量)是Roblox的父子階層的根。它的直接兒女是服務,例如 Workspace 和 Lighting ,這些服務是 Roblox 遊戲的基本組成部分。
範例程式碼
Demonstrates using game, the root instance of DataModel, to get services such as Workspace and Lighting.
local Workspace = game:GetService("Workspace")
local Lighting = game:GetService("Lighting")
-- Examples of modifying properties of these services
Workspace.Gravity = 20
Lighting.ClockTime = 4
概要
屬性
描述擁有空間方的用戶或群組的識別符。
描述空間方的 Enum.CreatorType,地方是由使用者或群組擁有還是否擁有。
描述服務器上運行的地方所屬的體驗ID。
無法使用。歷史上,將地方的 Enum.Genre 設為 Roblox 網站上的設置。
運行中遊戲伺服器實個體、實例的獨特標示符。
代表服務器上玩家如何通過匹配來處理。
描述伺服器上運行的地方的 ID。
描述伺服器運行的地方版本。
描述服務器的私人伺服器ID,如果服務器是私人伺服器或是reserved server。
描述擁有私人伺服器的 如果伺服器是私人的。
對 Workspace 服務的參考。
方法
將功能綁定到服務器關閉之前呼叫。
返回包含任務排程器執行的基本信息的表。
返回與給定內容 URL 相關的 Instances 數組。
如果客戶端第一次載入遊戲完成,返回真值。
將現有遊戲實例的 DataModel.PlaceId 設置為指定的 placeId。
將現有遊戲實例的 DataModel.GameId 設置為指定的 宇宙ID。
方法
如果給定的 className 已創建服務,返回指定的服務,如果名稱無效,錯誤。
返回要求的類別名稱的服務,如果不存在,則創建它。
活動
當使用者提示時發生火災,並使用熱鍵增加或減少圖形品質。
當遊戲第一次載入完成時,在客戶端發生火災。
活動
當現有位置退出時發生火災。
在服務創建時發射。
在服務即將被移除時發射。
屬性
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("The place owner has joined the game!")
end
elseif game.CreatorType == Enum.CreatorType.Group then
if player:IsInGroup(game.CreatorId) then
print("A member of the group that owns the place has joined the game!")
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("The place owner has joined the game!")
end
elseif game.CreatorType == Enum.CreatorType.Group then
if player:IsInGroup(game.CreatorId) then
print("A member of the group that owns the place has joined the game!")
end
end
end)
Environment
GameId
JobId
MatchmakingType
PlaceId
PlaceVersion
範例程式碼
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("Server version: %s", placeVersion)
textLabel.Parent = versionGui
versionGui.Parent = StarterGui
PrivateServerId
範例程式碼
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())
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
-- Avoid writing studio data to production and stalling test session closing
return
end
-- Reference for yielding and resuming later
local mainThread = coroutine.running()
-- Counts up for each new thread, down when the thread finishes. When 0 is reached,
-- the individual thread knows it's the last thread to finish and should resume the main thread
local numThreadsRunning = 0
-- Calling this function later starts on a new thread because of coroutine.wrap
local startSaveThread = coroutine.wrap(function(userId, sessionData)
-- Perform the save operation
local success, result = pcall(savePlayerDataAsync, userId, sessionData)
if not success then
-- Could implement a retry
warn(string.format("Failed to save %d's data: %s", userId, result))
end
-- Thread finished, decrement counter
numThreadsRunning -= 1
if numThreadsRunning == 0 then
-- This was the last thread to finish, resume main thread
coroutine.resume(mainThread)
end
end)
-- This assumes playerData gets cleared from the data table during a final save on PlayerRemoving,
-- so this is iterating over all the data of players still in the game that hasn't been saved
for userId, sessionData in pairs(allPlayerSessionDataCache) do
numThreadsRunning += 1
-- This loop finishes running and counting numThreadsRunning before any of
-- the save threads start because coroutine.wrap has built-in deferral on start
startSaveThread(userId, sessionData)
end
if numThreadsRunning > 0 then
-- Stall shutdown until save threads finish. Resumed by the last save thread when it finishes
coroutine.yield()
end
end
game:BindToClose(onServerShutdown)
game:BindToClose(function(closeReason)
print(`Closing with reason {closeReason}`)
task.wait(3)
print("Done")
end)
GetJobsInfo
返回
範例程式碼
local jobInfo = game:GetJobsInfo()
local jobTitles = jobInfo[1]
table.remove(jobInfo, 1)
local divider = string.rep("-", 120)
print(divider)
warn("JOB INFO:")
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)
end
GetObjects
參數
返回
範例程式碼
local Selection = game:GetService("Selection")
local WEB_URL = "plugin URL here"
local function downloadPlugin(webURL)
-- get the content URL
local contentID = string.match(webURL, "%d+")
local contentURL = "rbxassetid://" .. contentID
-- download the objects
local objects = game:GetObjects(contentURL)
-- decide where to parent them
local selection = Selection:Get()
local parent = #selection == 1 and selection[1] or workspace
-- parent the objects
for _, object in pairs(objects) do
object.Parent = parent
end
end
downloadPlugin(WEB_URL)
local IMAGES = {
-- 在此處插入裝飾網頁URL(作為字串)
}
-- 開啟辭典
local outputString = "textures = {"
-- 增加辭典新項目的實用功能(作為字串)
local function addEntryToDictionary(original, new)
outputString = outputString
.. "\n" -- 新行
.. " " -- indent 列印
.. '["'
.. original
.. '"]' -- 關鍵
.. ' = "'
.. new
.. '",' -- 值
end
print("Starting conversion")
for _, webURL in pairs(IMAGES) do
-- 取得內容 URL
local contentID = string.match(webURL, "%d+")
local contentURL = "rbxassetid://" .. contentID
local success, result = pcall(function()
local objects = game:GetObjects(contentURL)
return objects[1].Texture
end)
if success then
addEntryToDictionary(webURL, result)
else
addEntryToDictionary(webURL, "Error downloading decal")
end
task.wait()
end
print("Conversion complete")
-- 關閉辭典
outputString = outputString .. "\n}"
-- 列印辭典
print(outputString)
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 = "Loading"
textLabel.TextSize = 28
textLabel.Parent = screenGui
-- 將整個父畫面 GUI 轉換為玩家 GUI
screenGui.Parent = playerGui
-- 移除預設載入屏幕
ReplicatedFirst:RemoveDefaultLoadingScreen()
--等待(3) -- 可選擇強制屏幕在最少數秒內出現
if not game:IsLoaded() then
game.Loaded:Wait()
end
screenGui:Destroy()
活動
GraphicsQualityChangeRequest
參數
範例程式碼
game.GraphicsQualityChangeRequest:Connect(function(betterQuality)
if betterQuality then
print("The user has requested an increase in graphics quality!")
else
print("The user has requested a decrease in graphics quality!")
end
end)