Engine Class
DataModel
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
คุณสมบัติ
วิธีการ
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 |
สมาชิกที่ได้รับทอด
สมาชิก 7 คนที่รับทอดมาจาก ServiceProvider
สมาชิก 57 คนที่รับทอดมาจาก Instance
สมาชิก 6 คนที่รับทอดมาจาก Object
ตัวอย่างโค้ด
GetService()
local Workspace = game:GetService("Workspace")
local Lighting = game:GetService("Lighting")
-- ตัวอย่างการปรับคุณสมบัติของบริการนี้
Workspace.Gravity = 20
Lighting.ClockTime = 4เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
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,
-- ดังนั้นนี่คือการวนซ้ำผ่านข้อมูลทั้งหมดของผู้เล่นที่ยังอยู่ในเกมที่ยังไม่ได้ถูกบันทึก
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 เป็นจริง
restartPending = true
end
end)Callbacks
OnClose