RomoteEventの使用例
localscriptとserverscriptの間で相互の呼び出しに使われるRemoteEvent。
ReplicatedStorageに配置して、LocalScriptからもアクセスできるようにすることが多い。
RemoteEvents: Client to Server
-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = ReplicatedStorage:WaitForChild("CreatePartEvent")
createPartEvent:FireServer(BrickColor.Green(), Vector3.new(10, 20, 0))
-- ========================================
-- Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
createPartEvent.Name = "CreatePartEvent"
local function onCreatePartFired(player, color, position)
print(player.Name, "wants to create a part")
local newPart = Instance.new("Part")
newPart.BrickColor = color
newPart.Position = position
newPart.Parent = game.Workspace
end
createPartEvent.OnServerEvent:Connect(onCreatePartFired)
RemoteEvents: Server to a Single Client
-- Script
local Players = game:GetService("Players")
local welcomePlayerEvent = Instance.new("RemoteEvent")
welcomePlayerEvent.Parent = game.ReplicatedStorage
welcomePlayerEvent.Name = "WelcomePlayerEvent"
local function onPlayerAdded(player)
welcomePlayerEvent:FireClient(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- ========================================
--LocalScript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("WelcomePlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local welcomeMessage = Instance.new("TextLabel")
welcomeMessage.Size = UDim2.new(0, 200, 0, 50)
welcomeMessage.Parent = welcomeScreen
welcomeMessage.Visible = false
welcomeMessage.Text = "Welcome to the game!"
local function onWelcomePlayerFired()
welcomeMessage.Visible = true
wait(3)
welcomeMessage.Visible = false
end
welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)
RemoteEvents: Server to Multiple Clients
-- Script
local Players = game:GetService("Players")
local newPlayerEvent = Instance.new("RemoteEvent")
newPlayerEvent.Parent = game.ReplicatedStorage
newPlayerEvent.Name = "NewPlayer"
local function onPlayerAdded(player)
newPlayerEvent:FireAllClients()
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- ========================================
--LocalScript (for each client)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local newPlayerEvent = ReplicatedStorage:WaitForChild("NewPlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local newPlayerMessage = Instance.new("TextLabel")
newPlayerMessage.Size = UDim2.new(0, 200, 0, 50)
newPlayerMessage.Parent = welcomeScreen
newPlayerMessage.Visible = false
newPlayerMessage.Text = "A new player has joined the game!"
local function onNewPlayerFired()
newPlayerMessage.Visible = true
wait(3)
newPlayerMessage.Visible = false
end
newPlayerEvent.OnClientEvent:Connect(onNewPlayerFired)
参考
https://developer.roblox.com/en-us/api-reference/class/RemoteEvent