# Luau入門6：イベントとConnect（〜したときに動かす）

> ロブロックス（Roblox）のスクリプトは、「ふれたとき」「プレイヤーが参加したとき」などのイベントに、Connectで関数をつないで動かします。

- URL：https://roblox.minecraft-okinawa.com/luau/events/
- 更新日：2026-11-13

## イベントに関数をつなぐ

```lua
local part = script.Parent :: BasePart

part.Touched:Connect(function(hit)
	local character = hit.Parent
	if not character then
		return
	end
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end)
```


「part.Touched」がイベントで、「:Connect(関数)」でつなぎます。イベントが起きるたびに、つないだ関数が実行されます。

## よく使うイベント

| イベント | 起きるとき |
|---|---|
| BasePart.Touched | パーツに何かがふれたとき |
| Players.PlayerAdded | プレイヤーが参加したとき |
| Player.CharacterAdded | キャラクターが出現したとき |
| Humanoid.Died | キャラクターがやられたとき |
| ClickDetector.MouseClick | パーツがクリックされたとき |
| ProximityPrompt.Triggered | 「Eキーで調べる」が押されたとき |

## 1回だけ反応させる

```lua
local part = script.Parent :: BasePart
local connection

connection = part.Touched:Connect(function(hit: BasePart)
	print(hit.Name .. " がふれた")
	connection:Disconnect()
end)
```


Connectの戻り値を変数に入れておき、Disconnectを呼ぶと、つなぎを切れます。

## 出典

- [Luau 公式サイト](https://luau.org/)
- [Roblox Creator Documentation](https://create.roblox.com/docs)

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