Empty
Empty
Empty
ReplicatedStorage 是一個可以存放對象和腳本的地方,這些對象和腳本需要由伺服器和玩家的計算機(客戶端)訪問。
Empty
Empty
Empty
它有助於在伺服器和玩家之間共享常見資產,如工具、模塊和數據,確保每個人都能訪問相同的資源。
ModuleScript (模塊腳本) 在 ReplicatedStorage 中
Empty
Empty
Empty
-- ReplicatedStorage > SharedModule
local SharedModule = {}
function SharedModule.greet(name)
return "Hello, " .. name .. "!"
end
return SharedModule
Empty
Empty
Empty
-- ServerScriptService Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SharedModule = require(ReplicatedStorage:WaitForChild("SharedModule"))
print(SharedModule.greet("Server"))
Empty
Empty
Empty
-- StarterPlayerScripts LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SharedModule = require(ReplicatedStorage:WaitForChild("SharedModule"))
print(SharedModule.greet("Client"))
Remote Event (遠程事件) 在 ReplicatedStorage 中
Empty
Empty
Empty
-- StarterPlayerScripts LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ExampleEvent = ReplicatedStorage:WaitForChild("ExampleEvent")
local function onEventFired(message)
print(message)
end
ExampleEvent.OnClientEvent:Connect(onEventFired)
Empty
Empty
Empty
-- ServerScriptService Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ExampleEvent = ReplicatedStorage:WaitForChild("ExampleEvent")
local function fireEvent()
ExampleEvent:FireAllClients("Hello from the server!")
end
fireEvent()
Empty
Empty
Empty
-- ServerScriptService Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ExampleEvent = ReplicatedStorage:WaitForChild("ExampleEvent")
local function fireEvent(player)
ExampleEvent:FireClient(player, "Hello from the server!")
end
game.Players.PlayerAdded:Connect(function(player)
fireEvent(player)
end)
ScreenGui (屏幕 GUI) 在 ReplicatedStorage 中
Empty
Empty
Empty
-- StarterPlayerScripts LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local playerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local ShowWelcomeGuiEvent = ReplicatedStorage:WaitForChild("ShowWelcomeGuiEvent")
local function displayGui(playerName)
local guiTemplate = ReplicatedStorage:WaitForChild("WelcomeGuiTemplate")
local guiClone = guiTemplate:Clone()
local textLabel = guiClone:FindFirstChild("TextLabel")
if textLabel then
textLabel.Text = "Welcome, " .. playerName
end
guiClone.Parent = playerGui
end
-- Connect the function to the RemoteEvent
ShowWelcomeGuiEvent.OnClientEvent:Connect(displayGui)
Empty
Empty
Empty
-- StarterPlayerScripts LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local playerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local ShowWelcomeGuiEvent = ReplicatedStorage:WaitForChild("ShowWelcomeGuiEvent")
local function displayGui(playerName)
local guiTemplate = ReplicatedStorage:WaitForChild("WelcomeGuiTemplate")
local guiClone = guiTemplate:Clone()
local textLabel = guiClone:FindFirstChild("TextLabel")
if textLabel then
textLabel.Text = "Welcome, " .. playerName
end
guiClone.Parent = playerGui
end
-- Connect the function to the RemoteEvent
ShowWelcomeGuiEvent.OnClientEvent:Connect(displayGui)
Roblox Studio
Empty
Empty
Empty
- 存儲在 ReplicatedStorage 中的 GUI 元素在需要之前不會被加載到內存或被客戶端處理。
- PlayerGui 中的 GUI 元素消耗內存和 CPU 資源,包括那些不可見的元素。