只能在伺服器上存取內容的容器。從伺服器儲存下降的對象不會複製到客戶端,也不會從 LocalScripts 存取。
由於 ServerStorage 是一個服務,只能使用 DataModel.GetService 方法進行存取。
將大型對象,例如地圖,存儲在伺服器存儲中,直到它們被需要,網絡流量不會被用盡,當它們加入遊戲時,將這些對象傳送給客戶端。
Scripts 在他們被指派到伺服器儲存時不會運行,雖然 ModuleScripts 包含在內可以存取和運行。建議開發人員使用 ServerScriptService 來保留 Scripts 他們希望服務器執行的內容。
請注意,由於 ServerStorage 的內容只能由服務伺服器存取,因此其內容必須在客戶端存取之前(例如 Workspace)轉到其他地方才能使用。建議使用 ReplicatedStorage 來取代那些需要服務器和客戶兩者都能訪問的容器。
範例程式碼
This is a very simple example of how a multiple map system can be made using ServerStorage.
It creates three dummy models (to take the place of maps), that are parented to ServerStorage. Then, it will load a random map (different to the previous map) and wait a specified duration on a loop.
Developers wishing to integrate something similar into their games should consider measures to ensure players respawn correctly, or go to a lobby during intermission periods.
local ServerStorage = game:GetService("ServerStorage")
local ROUND_TIME = 5
local map1 = Instance.new("Model")
map1.Name = "Map1"
map1.Parent = ServerStorage
local map2 = Instance.new("Model")
map2.Name = "Map2"
map2.Parent = ServerStorage
local map3 = Instance.new("Model")
map3.Name = "Map3"
map3.Parent = ServerStorage
local maps = { map1, map2, map3 }
local RNG = Random.new()
local currentMap = nil
local lastPick = nil
while true do
print("New map!")
-- remove current map
if currentMap then
currentMap:Destroy()
end
-- pick a map
local randomPick = nil
if #maps > 1 then
repeat
randomPick = RNG:NextInteger(1, #maps)
until randomPick ~= lastPick
lastPick = randomPick
end
-- fetch new map
local map = maps[randomPick]
currentMap = map:Clone()
currentMap.Parent = workspace
task.wait(ROUND_TIME)
end