# RemoteEvent（ロブロックス クラス）

> サーバーとクライアント（プレイヤーのコンピューター）のあいだで、合図とデータを送り合うための部品です。

- URL：https://roblox.minecraft-okinawa.com/reference/classes/RemoteEvent/
- 公式：https://create.roblox.com/docs/reference/engine/classes/RemoteEvent
- 受け継いでいるクラス：BaseRemoteEvent

LocalScriptで起きたこと（ボタンを押したなど）をサーバーに伝えたり、サーバーから特定のプレイヤーに知らせたりするときに使います。ReplicatedStorageに置くのが基本です。クライアントから送られてきた値は、そのまま信用せずにサーバーで確かめましょう。

## 使用例：RemoteEventを作って受け取る（Script）

（Script／置く場所：ServerScriptService）

```lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remote = Instance.new("RemoteEvent")
remote.Name = "SayHello"
remote.Parent = ReplicatedStorage

remote.OnServerEvent:Connect(function(player, message)
	print(player.Name .. "：" .. tostring(message))
end)
```

## 使用例：RemoteEventで送る（LocalScript）

（LocalScript／置く場所：StarterPlayerScripts）

```lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("SayHello") :: RemoteEvent

remote:FireServer("こんにちは")
```

## メソッド

- `RemoteEvent:FireAllClients(arguments: Tuple): ()`：サーバーから全員へ合図を送ります。
- `RemoteEvent:FireClient(player: Player, arguments: Tuple): ()`：サーバーから特定のプレイヤーへ合図を送ります。
- `RemoteEvent:FireServer(arguments: Tuple): ()`：クライアントからサーバーへ合図を送ります（LocalScriptで使う）。

## イベント

- `RemoteEvent.OnClientEvent(arguments: Tuple)`：クライアント側で、サーバーからの合図を受け取るイベントです。
- `RemoteEvent.OnServerEvent(player: Player, arguments: Tuple)`：サーバー側で、クライアントからの合図を受け取るイベントです。1つ目の引数は送ってきたプレイヤーです。

英語の説明とメンバー一覧：Roblox Creator Documentation（CC BY 4.0）。日本語の解説はこのサイト独自のものです。

執筆：鈴木孝昌（現役プロジェクトマネージャー／プログラミング講師）／運営：沖縄マイクラ部 プログラミングスクール「クロスウェーブ」（https://minecraft-okinawa.com/）