RobloxでプレイヤーのHumanoidの状態が変更されたことを検知する
HumanoidStateとは
RobloxのHumanoid型のプレイヤーは自身の状態を持っている、例えば走っている状態であったり、自由落下している状態、泳いでいる状態でなどである。
主な状態をいくつか上げると以下ものがある(すべての状態は関連リンクを参照)
| FallingDown | 自由落下 |
| Running | 走っている |
| Climbing | 登っている |
| Jumping | ジャンプした |
| Landed | 自由落下から地面に着地した |
| Dead | 死亡した |
| Swimming | 泳いでいる |
これらの状態を検知することでHumanoidの状態に応じた演出や処理を実行することができる。例えば着地時の演出であったり、2段ジャンプ機能であったりする。
HumanoidStateの変更の検知のサンプル
HumanoidStateの状態を検知するにはHumanoid.StateChangedを利用する。
検知用のローカルスクリプトの作成
StarterPlayer -> StarterPlayerScriptsの配下にローカルスクリプトを作成、名前をDetectHumanoidStateChangedに変更する

ローカルスクリプトを以下のように編集する
local localPlayer = game.Players.LocalPlayer
-- キャラクターが追加されたときに実行
local function characterAdded(newCharacter)
local character = newCharacter
local humanoid = newCharacter:WaitForChild("Humanoid")
-- humanoidの状態の変更を購読する
humanoid.StateChanged:Connect(function(oldState, newState)
-- ジャンプしたときのみログを出力する
if newState ~= Enum.HumanoidStateType.Jumping then
return
end
print("Jump!!")
end)
end
if localPlayer.Character then
-- キャラクターが追加されているなら追加用関数を実行
characterAdded(localPlayer.Character)
else
-- キャラクターが追加されていない場合は追加時のイベントを購読
localPlayer.CharacterAdded:connect(characterAdded)
end
結果
ジャンプするたびにログが出力される

関連
https://developer.roblox.com/en-us/api-reference/enum/HumanoidStateType
https://developer.roblox.com/en-us/api-reference/event/Humanoid/StateChanged