DataModel
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
โมเดลข้อมูล (ที่รู้จักกันทั่วไปว่าเป็น หลังจากแปรโลกที่ใช้เพื่อเข้าถึงมัน) คือรากของห่วงโซ่ความสัมพันธ์แบบพ่อ-ลูกของ 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 ของสถานที่ว่าสถานที่เป็นเจ้าของโดยผู้ใช้หรือกลุ่ม
อธิบายรหัสประสบการณ์ที่สถานที่ที่ทำงานบนเซิร์ฟเวอร์นั้นเป็นส่วนหนึ่ง
ไม่สามารถใช้งานได้ ในอดีตบรรยาย Enum.Genre ของสถานที่ที่กำหนดไว้บนเว็บไซต์ Roblox
ตัวระบุเฉพาะสำหรับอินสแตนซ์เซิร์ฟเวอร์เกมที่กำลังทำงาน
การค้นหาแมตช์
อธิบายรหัสของสถานที่ที่ทำงานบนเซิร์ฟเวอร์
อธิบายเวอร์ชันของสถานที่ที่เซิร์ฟเวอร์กำลังทำงานอยู่
อธิบาย ID เซิร์ฟเวอร์ส่วนตัวของเซิร์ฟเวอร์ หากเซิร์ฟเวอร์เป็นเซิร์ฟเวอร์ส่วนตัวหรือ reserved server
อธิบาย UserId ของ Player ที่เป็นเจ้าของเซิร์ฟเวอร์ส่วนตัวหากเซิร์ฟเวอร์เป็นส่วนตัว
การอ้างอิงถึงบริการ Workspace
วิธีการ
ผูกฟังก์ชันที่จะโทรก่อนที่เซิร์ฟเวอร์จะปิด
คืนตารางที่มีข้อมูลพื้นฐานเกี่ยวกับงานที่ดำเนินการโดยตัวจัดการกำหนดเวลา
ส่งคืนคอลเลกชันของ Instances ที่เกี่ยวข้องกับ URL เนื้อหาที่กำหนด
ส่งคืนค่าจริงหากไคลเอนต์เสร็จสิ้นการโหลดเกมเป็นครั้งแรก
ตั้งค่า DataModel.PlaceId ของตัวอย่างเกมปัจจุบันไปยัง placeId ที่กำหนด
ตั้งค่า DataModel.GameId ของตัวอย่างเกมปัจจุบันไปยัง universeId ที่กำหนด
คืนบริการที่ระบุโดย className ที่กำหนดถ้ามันถูกสร้างขึ้นแล้ว ข้อผิดพลาดสำหรับชื่อที่ไม่ถูกต้อง
คืนบริการด้วยชื่อคลาสที่ร้องขอ โดยสร้างขึ้นถ้าไม่มีอยู่
อีเวนต์
เกิดไฟไหม้เมื่อผู้ใช้ร้องขอและเพิ่มหรือลดคุณภาพกราฟิกโดยใช้ปุ่มลัด
ไฟไหม้บนไคลเอนต์เมื่อเกมสิ้นสุดการโหลดครั้งแรก
เกิดไฟไหม้เมื่อสถานที่ปัจจุบันถูกออก
ยิงเมื่อบริการถูกสร้าง
ยิงเมื่อบริการกำลังจะถูกลบออก
คุณสมบัติ
CreatorId
คุณสมบัตินี้อธิบายรหัสของผู้ใช้หรือกลุ่มที่เป็นเจ้าของสถานที่หากคุณสมบัติ DataModel.CreatorType เป็น 'ผู้ใช้' แล้ว CreatorId จะเป็น Player.UserId ของเจ้าของสถานที่หาก DataModel.CreatorType เป็น 'กลุ่ม' แล้ว CreatorId จะเป็นรหัสกลุ่มที่เป็นเจ้าของสถานที่
ตัวอย่างโค้ด
This code sample will print an output when the user that owns the game, or a member of the group that owns the game joins the server.
To run this script, place it inside a Script in ServerScriptService
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
คุณสมบัตินี้อธิบายเกี่ยวกับ Enum.CreatorType ของสถานที่ว่าสถานที่เป็นเจ้าของโดยผู้ใช้หรือกลุ่ม
หาก Enum.CreatorType เป็น 'ผู้ใช้' คุณสมบัติ DataModel.CreatorId จะอธิบาย UserId ของบัญชีที่เป็นเจ้าของเกมหากประเภทผู้สร้างเป็น 'กลุ่ม' จะอธิบายรหัสกลุ่ม
ตัวอย่างโค้ด
This code sample will print an output when the user that owns the game, or a member of the group that owns the game joins the server.
To run this script, place it inside a Script in ServerScriptService
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
คุณสมบัตินี้อธิบายรหัสประสบการณ์ที่สถานที่ที่ทำงานบนเซิร์ฟเวอร์นั้นเป็นส่วนหนึ่ง
ดูเพิ่มเติม
- DataModel.PlaceId ซึ่งอธิบายรหัสของสถานที่ที่ทำงานบนเซิร์ฟเวอร์
- DataModel.JobId ซึ่งเป็นตัวระบุที่ไม่ซ้ำกันสำหรับตัวอย่างเกมเซิร์ฟเวอร์ที่กำลังดำเนินอยู่
- TeleportService ซึ่งเป็นบริการที่สามารถใช้เพื่อขนส่ง Players ระหว่างเกม
Genre
คุณสมบัตินี้มีข้อบกพร่องและไม่ควรใช้
คุณสมบัตินี้อธิบายประวัติศาสตร์ของสถานที่ Enum.Genre ตามที่กำหนดไว้บนเว็บไซต์ Roblox
คุณสมบัตินี้ร่วมกับ DataModel.GearGenreSetting ไม่สามารถทำงานได้อย่างถูกต้องอีกต่อไปเนื่องจากแนวเพลงที่มีอยู่บนเว็บไซต์ Roblox ที่ไม่สะท้อนในรายการเลือก ผลการค้นหา
JobId
คุณสมบัตินี้เป็นตัวระบุที่ไม่ซ้ำกันสำหรับอินสแตนซ์เซิร์ฟเวอร์เกมที่กำลังทำงานเป็นตัวระบุที่ไม่ซ้ำกันทั่วไป (UUID) ซึ่งหมายความว่าไม่มีเซิร์ฟเวอร์สองตัวที่ผ่านมาหรือปัจจุบันจะมีรหัสเดียวกัน
ค่าเริ่มต้นเป็นสตริงว่างเปล่าใน Studio
ดูเพิ่มเติม
- TeleportService:GetPlayerPlaceInstanceAsync() ซึ่งสามารถใช้เพื่อดึง DataModel.JobId ของเซิร์ฟเวอร์ปัจจุบันของผู้ใช้
- TeleportService:TeleportToPlaceInstance() ซึ่งสามารถใช้เพื่อเทเลพอร์ต Player ไปยังเซิร์ฟเวอร์เฉพาะ
- DataModel.PrivateServerId อธิบายรหัสของเซิร์ฟเวอร์ส่วนตัวที่อินสแตนซ์เซิร์ฟเวอร์เกมเป็นส่วนหนึ่ง
- HttpService:GenerateGUID() ฟังก์ชันที่สามารถใช้เพื่อสร้าง UUID ของคุณเอง
MatchmakingType
คุณสมบัตินี้แสดงถึงวิธีที่ผู้เล่นในเซิร์ฟเวอร์จะถูกจัดการโดยการจับคู่ผู้เล่นที่มีความแตกต่าง MatchmakingTypes การค้นหาแมตช์
โปรดทราบว่าคุณสมบัตินี้ใช้ได้เฉพาะในเซิร์ฟเวอร์ DataModel และจะเป็นค่า Enum.MatchmakingType.Default สำหรับลูกค้าทั้งหมด ดังนั้นจึงควรอ้างถึงคุณสมบัตินี้ภายในเซิร์ฟเวอร์เท่านั้น Script
PlaceId
คุณสมบัตินี้อธิบายรหัสของสถานที่ที่ทำงานบนเซิร์ฟเวอร์
หากสถานที่ยังไม่ถูกเผยแพร่ไปยัง Roblox รหัสนี้จะตรงกับเทมเพลตที่ใช้อยู่
ดูเพิ่มเติม
- DataModel.GameId ซึ่งอธิบายรหัสประสบการณ์ที่สถานที่ปัจจุบันเป็นส่วนหนึ่ง
- DataModel.JobId ซึ่งเป็นตัวระบุที่ไม่ซ้ำกันสำหรับตัวอย่างเกมเซิร์ฟเวอร์ที่กำลังดำเนินอยู่
- TeleportService ซึ่งเป็นบริการที่สามารถใช้เพื่อขนส่ง Players ระหว่างสถานที่
PlaceVersion
คุณสมบัตินี้อธิบายเวอร์ชันของสถานที่ที่เซิร์ฟเวอร์กำลังทำงานอยู่
หมายเลขรุ่นนี้สอดคล้องกับหมายเลขรุ่นที่แสดงภายใต้ส่วน ประวัติเวอร์ชัน ของการตั้งค่าสถานที่ไม่ใช่เวอร์ชันปัจจุบันของไคลเอนต์ Robloxคุณสมบัตินี้คือ 0 สำหรับประสบการณ์ที่ไม่ได้เผยแพร่ทั้งหมด
เมื่อสร้างตัวอย่างเซิร์ฟเวอร์สำหรับสถานที่ จะใช้เวอร์ชันปัจจุบันของสถานที่หากสถานที่ถูกอัปเดตในภายหลังในขณะที่เซิร์ฟเวอร์กําลังทํางานอยู่ เซิร์ฟเวอร์จะยังคงอยู่ในรุ่นปัจจุบัน
คุณสมบัตินี้สามารถใช้เพื่อแสดง ScreenGui เวอร์ชันปัจจุบันของเกมให้กับ Players เพื่อช่วยในการดีบัก
ตัวอย่างโค้ด
This code sample will place a simple GUI in the StarterGui showing the place version the server is running at.
To use this sample, place it inside a Script in ServerScriptService.
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
คุณสมบัตินี้อธิบายรหัสเซิร์ฟเวอร์ส่วนตัวของเซิร์ฟเวอร์หากเซิร์ฟเวอร์เป็นเซิร์ฟเวอร์ส่วนตัว
หากเซิร์ฟเวอร์ไม่ใช่เซิร์ฟเวอร์ส่วนตัว คุณสมบัตินี้จะเป็นสตริงว่างเปล่า
เซิร์ฟเวอร์ส่วนตัว
กำลังติดตาม:
- เซิร์ฟเวอร์ส่วนตัว ที่ผู้ใช้สามารถซื้อได้จากหน้าเกม
- เซิร์ฟเวอร์สำรอง เซิร์ฟเวอร์ส่วนตัวที่สร้างโดยนักพัฒนาโดยใช้ TeleportService:ReserveServer()
PrivateServerId กับ JobId
PrivateServerId ของเซิร์ฟเวอร์แตกต่างจาก DataModel.JobId อัตราการระบุที่ไม่ซ้ำกันของตัวอย่างเซิร์ฟเวอร์ปัจจุบันคือ JobId
เซิร์ฟเวอร์ส่วนตัว (เซิร์ฟเวอร์ส่วนตัวหรือเซิร์ฟเวอร์สำรอง) สามารถมีหลายตัวอย่างเซิร์ฟเวอร์ที่เกี่ยวข้องกับพวกเขาในเวลาที่ผ่านมาเนื่องจากแม้จะมีเพียงตัวอย่างเซิร์ฟเวอร์เดียวที่สามารถทำงานได้ในครั้งเดียวสำหรับเซิร์ฟเวอร์ส่วนตัว แต่ตัวอย่างเซิร์ฟเวอร์ใหม่สามารถเปิดและปิดได้เมื่อผู้เล่นเข้าร่วมและออกจากเกมตัวอย่างเช่น ไม่มีตัวอย่างเซิร์ฟเวอร์ที่ทำงานเมื่อไม่มีใครเล่นในเซิร์ฟเวอร์PrivateServerId จะสอดคล้องกันทั่วทั้งตัวอย่างเซิร์ฟเวอร์เหล่านี้, และ DataModel.JobId จะเป็นเอกลักษณ์สำหรับแต่ละตัว
ดูเพิ่ม:
- DataModel.PrivateServerOwnerId , คุณสมบัติที่อธิบายเจ้าของของเซิร์ฟเวอร์ส่วนตัว
- TeleportService:ReserveServer() ฟังก์ชันที่สร้างเซิร์ฟเวอร์สำรอง
ตัวอย่างโค้ด
DataModel.PrivateServerId and DataModel.PrivateServerOwnerId can be used to detect if the current server instance is a standard server, a VIP server or a reserved server.
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
คุณสมบัตินี้อธิบาย ของ ที่เป็นเจ้าของเซิร์ฟเวอร์ส่วนตัวหากเซิร์ฟเวอร์เป็นส่วนตัว
หากเซิร์ฟเวอร์เป็นเซิร์ฟเวอร์มาตรฐานหรือสำรองแล้วคุณสมบัตินี้จะถูกตั้งค่าเป็น 0
คุณสมบัตินี้สามารถใช้เพื่อระบุว่า Player เป็นเจ้าของเซิร์ฟเวอร์ส่วนตัวหรือไม่, ตัวอย่างเช่น:
local Players = game:GetService("Players")
-- นี่คือเซิร์ฟเวอร์ส่วนตัวหรือไม่?
if game.PrivateServerId ~= "" and game.PrivateServerOwnerId ~= 0 then
-- ฟังเพื่อรับผู้เล่นใหม่ที่ถูกเพิ่ม
Players.PlayerAdded:Connect(function(player)
-- ตรวจสอบว่าผู้เล่นเป็นเจ้าของเซิร์ฟเวอร์หรือไม่
if player.UserId == game.PrivateServerOwnerId then
print("The private server owner has joined the game")
end
end)
end
ดูเพิ่ม:
- DataModel.PrivateServerId , คุณสมบัติที่อธิบาย ID ที่ไม่ซ้ำกันของส่วนตัวและ reserved servers
ตัวอย่างโค้ด
DataModel.PrivateServerId and DataModel.PrivateServerOwnerId can be used to detect if the current server instance is a standard server, a VIP server or a reserved server.
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())
วิธีการ
BindToClose
ผูกฟังก์ชันที่จะโทรก่อนที่เซิร์ฟเวอร์จะปิดหากฟังก์ชันที่ผูกไว้ยอมรับพารามิเตอร์ มันจะส่ง Enum.CloseReason ระบุเหตุผลสําหรับการปิดเซิร์ฟเวอร์
คุณสามารถผูกฟังก์ชันหลายอย่างโดยเรียก BindToClose() ซ้ำๆ ฟังก์ชันที่ผูกจะเรียกแบบพาราเลลและทำงานในเวลาเดียวกัน
เซิร์ฟเวอร์ประสบการณ์รอ 30 วินาทีสำหรับฟังก์ชันทั้งหมดที่ผูกไว้ให้หยุดทำงานก่อนที่จะปิดตัวลงหลังจาก 30 วินาที เซิร์ฟเวอร์จะปิดตัวแม้ว่าฟังก์ชันจะยังคงทำงานอยู่
เพื่อตรวจสอบว่าเซสชั่นปัจจุบันไม่ได้อยู่ใน Roblox Studio ให้ใช้ RunService:IsStudio()สิ่งนี้ป้องกันไม่ให้ฟังก์ชันที่ผูกไว้สำเร็จในการทดสอบแบบออฟไลน์
เมื่อคุณใช้ DataStoreService , คุณควรใช้ BindToClose เพื่อผูกฟังก์ชันที่บันทึกข้อมูลทั้งหมดที่ไม่ได้บันทึกไว้ใน DataStoresสิ่งนี้ป้องกันการสูญเสียข้อมูลหากเซิร์ฟเวอร์ถูกปิดโดยไม่คาดคิด
ดูเพิ่ม:
- Enum.CloseReason เนื่องจากเหตุผลสําหรับการปิดเซิร์ฟเวอร์ประสบการณ์
- PluginGui:BindToClose() ซึ่งผูกฟังก์ชันกับปุ่มปิด PluginGui
พารามิเตอร์
ฟังก์ชันที่เรียกก่อนที่เซิร์ฟเวอร์ประสบการณ์จะปิดหากฟังก์ชันที่ผูกไว้ยอมรับพารามิเตอร์ มันจะส่ง Enum.CloseReason ระบุเหตุผลสําหรับการปิดเซิร์ฟเวอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
The following code sample is an example of how DataModel:BindToClose() can be used to save player data in the event of a server shutdown. In this example, player data is stored in a dictionary named playerData with UserIds as keys.
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)
The following example prints the close reason to the output. It prints Done after three seconds, and then allows Roblox to shut down the experience server.
The close reason matches one of the values in Enum.CloseReason.
game:BindToClose(function(closeReason)
print(`Closing with reason {closeReason}`)
task.wait(3)
print("Done")
end)
GetJobsInfo
คืนตารางที่มีข้อมูลพื้นฐานเกี่ยวกับงานที่ดำเนินการโดยตัวจัดการกำหนดเวลา
ในการคำนวณตัวจัดกำหนดเวลางานเป็นระบบที่รับผิดชอบในการดำเนินงานภารกิจสําคัญในช่วงเวลาที่เหมาะสม
คุณยังสามารถค้นหาสถิติตัวจัดการภารกิจสดในหน้าต่างตัวจัดการภารกิจใน Roblox Studio ได้
รายการแรกในตารางที่กลับมาคือสารานุกรมอ้างอิงที่มีสถิติ (หรือหัวข้อ) ที่มีอยู่ มันอยู่ในรูปแบบต่อไปนี้:
{["name"] = "name",["averageDutyCycle"] = "averageDutyCycle",["averageStepsPerSecond"] = "averageStepsPerSecond",["averageStepTime"] = "averageStepTime",["averageError"] = "averageError",["isRunning"] = "isRunning",}
รายการต่อไปในตารางที่ส่งคืนคือสารานุกรมที่มีสถิติด้านบนสำหรับงานที่ดำเนินการโดยตัวจัดการการนัดหมาย ตัวอย่างเช่น:
{["name"] = "Heartbeat",["averageDutyCycle"] = 0,["averageStepsPerSecond"] = 0,["averageStepTime"] = 0,["averageError"] = 0,["isRunning"] = false,}
ดูเพิ่ม:
ส่งค่ากลับ
ตารางที่มีข้อมูลเกี่ยวกับงานที่ดำเนินการโดยตัวจัดการกำหนดเวลางานดูด้านบนสำหรับรูปแบบ
ตัวอย่างโค้ด
Here is an example of iterating over the job info.
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
วิธีนี้จะคืนเป็นคอลเลกชันของ Instances ที่เกี่ยวข้องกับ URL เนื้อหาที่กำหนดสามารถใช้เพื่อใส่เนื้อหาจากห้องสมุด Roblox ได้ไม่สามารถใส่ Sounds โดยใช้วิธีนี้ได้เนื่องจากพวกเขาไม่มี Instance ที่เกี่ยวข้องกับพวกเขาและมีเพียง URL เนื้อหาเท่านั้น
ไม่เหมือนกับ InsertService:LoadAsset() , DataModel:GetObjects() ไม่ต้องการให้สินทรัพย์ "เชื่อถือได้" หมายความว่าสินทรัพย์ไม่จำเป็นต้องเป็นเจ้าของโดยผู้ใช้ที่ล็อกอินหรือสร้างโดย Roblox เพื่อใส่อย่างไรก็ตาม หากสินทรัพย์ไม่ได้เป็นเจ้าของโดยผู้ใช้ที่ล็อกอิน มันจะต้องใช้ได้ฟรี
เนื่องจากบริบทความปลอดภัยของฟังก์ชันนี้จะใช้ได้เฉพาะโดยปลั๊กอินหรือแถบคําสั่งเท่านั้นสำหรับทางเลือกที่สามารถใช้ได้ใน Scripts และ LocalScripts ดู InsertService:LoadAsset()
พารามิเตอร์
URL เนื้อหาที่กำหนดไว้
ส่งค่ากลับ
ชุดของ Instances ที่เกี่ยวข้องกับ URL เนื้อหา
ตัวอย่างโค้ด
If you want to view a plugin's source code without installing it, you can use DataModel:GetObjects() to download the plugin. The code sample below includes a function that will take a plugin's website URL and insert the plugin into the currently selected Instance or the Workspace.
Due to GetObjects' security context, this function can only be used in the command line or in a plugin.
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)
The content ID of a Decal on the Roblox website is associated with a Decal Instance rather than the actual content ID of the texture.
The code below, will use DataModel:GetObjects() to insert Decal objects into place and read their Decal.Texture property to obtain the image content IDs.
To use this code sample, enter the web URLs of the decals you'd like to convert into the array named IMAGES (as strings). A dictionary with the converted textures will be outputted.
local IMAGES = {
-- Insert Decal web URLs in an array here (as strings)
}
-- open the dictionary
local outputString = "textures = {"
-- utility function to add a new entry to the dictionary (as a string)
local function addEntryToDictionary(original, new)
outputString = outputString
.. "\n" -- new line
.. " " -- indent
.. '["'
.. original
.. '"]' -- key
.. ' = "'
.. new
.. '",' -- value
end
print("Starting conversion")
for _, webURL in pairs(IMAGES) do
-- get the content 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")
-- close the dictionary
outputString = outputString .. "\n}"
-- print the dictionary
print(outputString)
IsLoaded
เมื่อทั้งหมดของการเริ่มต้น Instances ในเกมได้สิ้นสุดการเลียนแบบไปยังไคลเอนต์แล้ว ฟังก์ชันนี้จะส่งคืนความจริง
เว้นแต่ว่าพวกเขาจะถูกผูกไว้กับ ReplicatedFirst , LocalScripts ไม่ได้ทำงานจนกว่าเกมจะโหลดเสร็จส่วนตัดต่อต่อไปนี้จะทำงานจาก LocalScript ใน ReplicatedFirst จนกว่าเกมจะโหลดเสร็จ:
if not game:IsLoaded() thengame.Loaded:Wait()end
ดูเพิ่ม:
- คำสั่งสําเนา สําหรับสรุปรายละเอียดเพิ่มเติมเกี่ยวกับกระบวนการโหลด
- DataModel.Loaded เหตุการณ์ที่จะเกิดขึ้นเมื่อเกมโหลดแล้ว
- Instance:WaitForChild() , ฟังก์ชันที่สามารถใช้เพื่อรอให้ผู้เล่นคนหนึ่ง Instance ทำซ้ำโดยไม่ต้องรอให้เกมทั้งหมดโหลดเสร็จ
ส่งค่ากลับ
ว่าไคลเอนต์ได้โหลดเกมเสร็จสมบูรณ์เป็นครั้งแรกหรือไม่
ตัวอย่างโค้ด
This sample demonstrates a custom loading screen with a basic TextLabel. The code should be placed in a LocalScript within ReplicatedFirst. To expand on this sample with loading screen animations, see the Custom Loading Screens article.
local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
-- Create a basic loading screen
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
-- Parent entire screen GUI to player GUI
screenGui.Parent = playerGui
-- Remove the default loading screen
ReplicatedFirst:RemoveDefaultLoadingScreen()
--wait(3) -- Optionally force screen to appear for a minimum number of seconds
if not game:IsLoaded() then
game.Loaded:Wait()
end
screenGui:Destroy()
SetPlaceId
ฟังก์ชันนี้ตั้ง DataModel.PlaceId ของตัวอย่างเกมไปยัง placeId ที่กำหนด
การตั้งค่าทั้ง DataModel.PlaceId และ DataModel.GameId จำเป็นต้องใช้เพื่อเข้าถึง DataStoreService เมื่อสถานที่ไม่ได้เผยแพร่ ตัวอย่างเช่นไฟล์ .rbxl ท้องถิ่นดูด้านล่างสำหรับตัวอย่างโปรดทราบว่าการได้รับการเข้าถึง DataStoreService จาก Studio ต้องใช้การตั้งค่า เปิดใช้งานการเข้าถึง Studio ไปยังบริการ API จากแผง ความปลอดภัย ใน การตั้งค่าเกม
local DataStoreService = game:GetService("DataStoreService")-- เข้าถึง DataStore 'Data' โดยการตั้ง PlaceId เป็น placeId และ GameId เป็น universeIdgame:SetPlaceId(placeId)game:SetUniverseId(universeId)local dataStore = DataStoreService:GetDataStore("Data")
พารามิเตอร์
ID ที่จะตั้ง DataModel.PlaceId ไปยัง
ส่งค่ากลับ
SetUniverseId
ฟังก์ชันนี้ตั้ง DataModel.GameId ของตัวอย่างเกมปัจจุบันไปยัง universeId ที่กำหนดมีประโยชน์เมื่อทดสอบไฟล์ .rbxl ท้องถิ่นที่ยังไม่ได้เผยแพร่ไปยัง Roblox
เพื่อเข้าถึง DataStoreService ในสถานที่ที่ไม่เผยแพร่ ทั้ง DataModel:SetUniverseId() และ DataModel:SetPlaceId() จะต้องถูกตั้งค่า
พารามิเตอร์
ID ที่จะตั้ง DataModel.GameId ไปยัง
ส่งค่ากลับ
อีเวนต์
GraphicsQualityChangeRequest
เกิดไฟไหม้เมื่อผู้ใช้ร้องขอการเพิ่มหรือลดคุณภาพกราฟิกโดยใช้ปุ่มลัด
อีเวนต์นี้จะยิงภายใต้เงื่อนไขต่อไปนี้:
- หากผู้ใช้กด F10 อีเวนต์นี้จะเกิดขึ้นด้วยอาร์กิวเมนต์ betterQuality ของ true
- หากผู้ใช้กด ShiftF10 อีเวนต์นี้จะเกิดขึ้นด้วยอาร์กิวเมนต์ betterQuality ของ false
อีเวนต์นี้ไม่ให้ระดับคุณภาพกราฟิกปัจจุบันหรือครอบคลุมการอัปเดตทั้งหมดในคุณภาพกราฟิกตัวอย่างเช่น การเปลี่ยนแปลงที่ทำในเมนูหลบหนี GUI หลักไม่ได้ลงทะเบียน
คุณสามารถดึงการดำเนินการของผู้ใช้ Enum.SavedQualitySetting โดยใช้ UserGameSettings ด้วยสนิปต์ต่อไปนี้:
UserSettings():GetService("UserGameSettings").SavedQualityLevel
หากการตั้งค่ากราฟิกของผู้ใช้ถูกตั้งค่าเป็นอัตโนมัติแล้ว Enum.SavedQualitySetting จะเป็น Automaticขณะนี้ไม่มีวิธีให้นักพัฒนาได้รับคุณภาพของกราฟิกปัจจุบันอย่างเชื่อถือจากเครื่องของผู้ใช้
พารามิเตอร์
ว่าผู้ใช้ได้ร้องขอให้เพิ่ม ( true ) หรือลด ( false ) คุณภาพของกราฟิก
ตัวอย่างโค้ด
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)
Loaded
อีเวนต์นี้จะเกิดขึ้นบนไคลเอนต์เมื่อทุกสิ่งเริ่มต้น Instances ในเกมสิ้นสุดการเลียนแบบไปยังไคลเอนต์แล้ว
เว้นแต่ว่าพวกเขาจะถูกผูกไว้กับ ReplicatedFirst , LocalScripts ไม่ได้ทำงานจนกว่าเกมจะโหลดเสร็จส่วนตัดต่อต่อไปนี้จะทำงานจาก LocalScript ใน ReplicatedFirst จนกว่าเกมจะโหลดเสร็จ:
if not game:IsLoaded() thengame.Loaded:Wait()end
ดูเพิ่ม:
- คำสั่งสําเนา สําหรับสรุปรายละเอียดเพิ่มเติมเกี่ยวกับกระบวนการโหลด
- DataModel.Loaded เหตุการณ์ที่จะเกิดขึ้นเมื่อเกมโหลดแล้ว
- Instance:WaitForChild() , ฟังก์ชันที่สามารถใช้เพื่อรอให้ผู้เล่นคนหนึ่ง Instance ทำซ้ำโดยไม่ต้องรอให้เกมทั้งหมดโหลดเสร็จ