スクリプトでプレイヤーの体力回復速度を変更する
デフォルトのHealthスクリプトの確認
Robloxでは体力の管理をHealthスクリプトが行っており、このスクリプトではプレイヤーの体力を自動回復する機能を実装している
デフォルトのHelthスクリプトの内容
コードを呼んでみると体力が最大未満の場合、1秒間に最大体力の1/100を回復するようになっている
-- Gradually regenerates the Humanoid's Health over time. local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second. local REGEN_STEP = 1 -- Wait this long between each regeneration step. -------------------------------------------------------------------------------- local Character = script.Parent local Humanoid = Character:WaitForChild'Humanoid' -------------------------------------------------------------------------------- while true do while Humanoid.Health < Humanoid.MaxHealth do local dt = wait(REGEN_STEP) local dh = dt*REGEN_RATE*Humanoid.MaxHealth Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth) end Humanoid.HealthChanged:Wait() end
ダメージを受けると回復していく

Healthスクリプトの上書き
StarterCharacterScriptsの配下にHealthスクリプトを作成することでHealthの挙動を上書きすることができる

回復処理がなくなったため回復しなくなる

もともとのスクリプトを参考に回復速度を変更する
作成したHelthスクリプトを以下のように編集する
-- Gradually regenerates the Humanoid's Health over time. local REGEN_RATE = 1 / 10 -- この行のみ変更(回復割合を10倍にする) local REGEN_STEP = 1 -- Wait this long between each regeneration step. -------------------------------------------------------------------------------- local Character = script.Parent local Humanoid = Character:WaitForChild'Humanoid' -------------------------------------------------------------------------------- while true do while Humanoid.Health < Humanoid.MaxHealth do local dt = wait(REGEN_STEP) local dh = dt*REGEN_RATE*Humanoid.MaxHealth Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth) end Humanoid.HealthChanged:Wait() end

参考スクリプトのように1秒ごとではなく、継続的に回復させることもできる
-- 回復するスピード local RECOVERY_SPEED = 10 local Character = script.Parent local Humanoid = Character:WaitForChild'Humanoid' while true do while Humanoid.Health < Humanoid.MaxHealth do local dt = wait() -- フレームの経過時間に回復スピードを掛け算して回復量を求める local dh = dt*RECOVERY_SPEED Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth) end Humanoid.HealthChanged:Wait() end

関連