ReplicatedStorage (复制存储)

ReplicatedStorage 是一个可以存放对象和脚本的地方,这些对象和脚本需要由服务器和玩家的计算机(客户端)访问。

它有助于在服务器和玩家之间共享常见资产,如工具、模块和数据,确保每个人都能访问相同的资源。

ModuleScript (模块脚本) 在 ReplicatedStorage 中

-- ReplicatedStorage > SharedModule

local SharedModule = {}

function SharedModule.greet(name)
    return "Hello, " .. name .. "!"
end

return SharedModule
-- ServerScriptService Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SharedModule = require(ReplicatedStorage:WaitForChild("SharedModule"))

print(SharedModule.greet("Server"))
-- StarterPlayerScripts LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SharedModule = require(ReplicatedStorage:WaitForChild("SharedModule"))


print(SharedModule.greet("Client"))

Remote Event (远程事件) 在 ReplicatedStorage 中

-- StarterPlayerScripts LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ExampleEvent = ReplicatedStorage:WaitForChild("ExampleEvent")

local function onEventFired(message)
    print(message)
end

ExampleEvent.OnClientEvent:Connect(onEventFired)
-- ServerScriptService Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ExampleEvent = ReplicatedStorage:WaitForChild("ExampleEvent")

local function fireEvent()
    ExampleEvent:FireAllClients("Hello from the server!")
end

fireEvent()
-- 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 中

-- 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)
-- 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)
Image 1
Image 2
Roblox Studio
  • 存储在 ReplicatedStorage 中的 GUI 元素在需要之前不会被加载到内存或被客户端处理。
  • PlayerGui 中的 GUI 元素消耗内存和 CPU 资源,包括那些不可见的元素。

如果您觉得本教程对您有帮助,并且愿意支持我的工作,请考虑请我喝杯咖啡。

非常感谢您的支持!

请我喝咖啡