イベントとConnect:「〜したとき」に動かす
初級イベント更新日:2026年11月13日Luauの型チェック済み
ひとことで言うと
ロブロックス(Roblox)のスクリプトは、「ふれたとき(Touched)」「プレイヤーが参加したとき(PlayerAdded)」などのイベントに、Connectで関数をつなぐことで動きます。
この記事で使うAPI
基本の形
イベント:Connect(function(引数)
-- イベントが起きたときにしたいこと
end)
よく使うイベント
| イベント | いつ起きる? |
|---|---|
| BasePart.Touched | パーツに何かがふれたとき |
| Players.PlayerAdded | プレイヤーが参加したとき |
| Player.CharacterAdded | キャラクターが出現したとき |
| ClickDetector.MouseClick | パーツがクリックされたとき |
| ProximityPrompt.Triggered | 近くでボタンを押したとき |
| RunService.Heartbeat | 毎フレーム |
Touchedは何度も起きる
足がパーツにふれると、Touchedは短い時間に何度も起きます。1回だけ反応させたいときは、「デバウンス」という仕組み(処理中かどうかを覚えておく変数)を使います。
(Script/置く場所:パーツの中)
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)
つなぎを切る
(Script/置く場所:パーツの中)
local part = script.Parent :: BasePart
local connection
connection = part.Touched:Connect(function(hit: BasePart)
print(hit.Name .. " がふれた")
connection:Disconnect()
end)
Connectの戻り値を変数に入れておくと、Disconnectでつなぎを切れます。1回だけ反応させたいときは、Onceを使う方法もあります。