# Touchedが何回も反応してしまう

> ふれたときの処理が、一度に何回も動いてしまう問題の直し方（デバウンス）です。

- URL：https://roblox.minecraft-okinawa.com/troubleshooting/touched-fires-many-times/
- 更新日：2026-11-13

## 原因

キャラクターは、頭・胴体・手足など、たくさんのパーツでできています。それぞれがふれるたびにTouchedが起きるので、一度ふれただけで何回も反応します。

## 直し方：デバウンス

「処理中かどうか」を変数で覚えておき、処理中なら何もしないようにします。この考え方をデバウンスといいます。

（Script／置く場所：パーツの中）

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

part.Touched:Connect(function(hit)
	if debounce then
		return
	end
	local character = hit.Parent
	if not character or not character:FindFirstChildWhichIsA("Humanoid") then
		return
	end
	debounce = true
	task.wait(0.5)
	part.Transparency = 1
	part.CanCollide = false
	task.wait(3)
	part.Transparency = 0
	part.CanCollide = true
	debounce = false
end)
```


## プレイヤーごとに1回にしたいとき

テーブルに、もう反応したプレイヤーを記録します。

```lua
local touched = {}
local part = script.Parent

part.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player and not touched[player] then
		touched[player] = true
		print(player.Name .. "がふれました")
	end
end)
```

## 教室ブログの関連記事

- [Roblox Studioのスクリプト入門｜Luauの基本を学ぼう](https://minecraft-okinawa.com/roblox-studio-luau-basics/)（2026-11-11）
- [ゲームが動かない！プログラミングコンテストで学ぶデバッグの本当の価値](https://minecraft-okinawa.com/debugging-value-for-kids-okinawa/)（2026-09-14）

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