ForceFieldで無敵時間を適用する
RobloxではForceFieldを利用することでSpawn時に無敵時間を適用することができる
ForceFieldとは
いわゆるバリア、これが有効になっているときはHumanoid:TakeDamageによるダメージやExplosionによる爆破からプレイヤーを守る

ダメージブロックの作成
プレイヤーが触れたらダメージを受けるブロックを作成する

ForceFieldの適用
SpawnLocationを選択、プロパティのForceFieldに無敵時間の秒数を入力する

結果
スポーンしてから10秒間はダメージブロックに触れてもダメージを受けない

注意
ForceFieldが防ぐダメージはHumanoid:TakeDamageによるものだけであり、Humanoid.Healthに対して直接値を入力してダメージを与える場合は防御が機能しない。その時はキャラクターのモデルがDistanceFieldを持っているかどうかを判別して、持っている場合はHealthの更新を行わない、などの処理が必要。
-- パーツを取得 local part = script.Parent -- パーツに触れたときに呼び出される local function onPartTouch(otherPart) -- 触れた対象がプレイヤーかどうかを判断する local humanoid = otherPart.Parent:FindFirstChild("Humanoid") if humanoid then local characterModel = humanoid.Parent -- キャラクターがForceFieldを持っていなかったら体力を10減らす if not characterModel:FindFirstChildOfClass("ForceField") then humanoid.Health = humanoid.Health - 10 end end end -- パーツに触れたときのイベントを登録する part.Touched:Connect(onPartTouch)
関連
https://developer.roblox.com/en-us/api-reference/class/ForceField
https://developer.roblox.com/en-us/api-reference/class/Humanoid