Robloxでスクリプトを利用してForceFieldを適用し、一定時間無敵になる

RobloxではSpawnLocationでのパラメータを変更することでSpawn時にForceFieldを適用することができるが、適用時間が切れてしまうとダメージを受けるようになってしまう。そこでパーツに触れることでForceFieldを生成できるようにする。

パーツを作成

ブロックパーツを作成、色を変更し、名前をApplyForceFieldBlockに変更する

スクリプトの作成

パーツの配下にスクリプトを作成、名前をApplyForceFieldに変更する

スクリプトを以下のように編集する

-- パーツを取得
local part = script.Parent
-- パーツに触れたときに呼び出される
local function onPartTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if humanoid then
-- ForceFieldを持っていたら何もしない
if humanoid.Parent:FindFirstChild("ForceField") then
return
end
-- ForceFieldを作成して、プレイヤーに適用する
local forceField = Instance.new("ForceField")
forceField.Parent = humanoid.Parent
print("add field")
-- 5秒後にForceFieldを削除する
wait(5)
forceField:Destroy()
end
end
-- パーツに触れたときのイベントを登録する
part.Touched:Connect(onPartTouch)
-- パーツを取得 local part = script.Parent -- パーツに触れたときに呼び出される local function onPartTouch(otherPart) local humanoid = otherPart.Parent:FindFirstChild("Humanoid") if humanoid then -- ForceFieldを持っていたら何もしない if humanoid.Parent:FindFirstChild("ForceField") then return end -- ForceFieldを作成して、プレイヤーに適用する local forceField = Instance.new("ForceField") forceField.Parent = humanoid.Parent print("add field") -- 5秒後にForceFieldを削除する wait(5) forceField:Destroy() end end -- パーツに触れたときのイベントを登録する part.Touched:Connect(onPartTouch)
-- パーツを取得
local part = script.Parent
-- パーツに触れたときに呼び出される
local function onPartTouch(otherPart)    
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid then                
		-- ForceFieldを持っていたら何もしない
		if humanoid.Parent:FindFirstChild("ForceField") then
			return
		end

		-- ForceFieldを作成して、プレイヤーに適用する
		local forceField = Instance.new("ForceField")
		forceField.Parent = humanoid.Parent
		print("add field")

		-- 5秒後にForceFieldを削除する
		wait(5)
		forceField:Destroy()        
	end
end
-- パーツに触れたときのイベントを登録する
part.Touched:Connect(onPartTouch)

結果

関連