只能在服务器上访问内容的容器。从服务器存储下降的对象不会复制到客户端,也不会从 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