ForceField 保護 Class.Humanoid 受到傷害 通過 1> Class.Humanoid:TakeDamage1>
傷害和關節
ForceField 會影響它的父親。當父親為 Model 時,它保護所有從那個模型下降的 BaseParts。如果父親為 1> Class.BasePart1> ,零件的關節只會受保護,如果模
ForceField 只能保護 Humanoids 免受 Humanoid:TakeDamage() 方法造成的傷害。人形可以仍然受到設置 0> Class.Humanoid.Health0> 直接造成傷害。因此
視覺化
當 ForceField.Visible 設為真時,會建立粒子效果。一些規則決定這個效果會從哪裡發射:
- 當與 BasePart 親級時,效果會從零件的 BasePart.Position 發射。
範例程式碼
ForceField Instantiation
local Players = game:GetService("Players")
local FORCE_FIELD_DURATION = 15
local function giveForcefield(player, duration)
local character = player.Character
if character then
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = character
if duration then
task.delay(duration, function()
if forceField then
forceField:Destroy()
end
end)
end
end
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function(_character)
giveForcefield(player, FORCE_FIELD_DURATION)
end)
end
Players.PlayerAdded(onPlayerAdded)
概要
屬性
Visible
決定是否啟用 ForceField 粒子效果。設定此為 false 會使您能夠在下一個代碼示例中替換預設粒子效果為自訂效果。
範例程式碼
Custom ForceField Effect
local Players = game:GetService("Players")
local FORCE_FIELD_DURATION = 15
local function createCustomForcefield(player, duration)
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
-- find the torso
local torsoName = humanoid.RigType == Enum.HumanoidRigType.R15 and "UpperTorso" or "Torso"
local torso = character:FindFirstChild(torsoName)
if torso then
-- create a forcefield
local forceField = Instance.new("ForceField")
forceField.Visible = false -- not visible
-- create a particle effect
local particleEmitter = Instance.new("ParticleEmitter")
particleEmitter.Enabled = true
particleEmitter.Parent = torso
-- listen for the forcefield being removed
forceField.AncestryChanged:Connect(function(_child, parent)
if not parent then
if particleEmitter and particleEmitter.Parent then
particleEmitter:Destroy()
end
end
end)
-- parent the forcefield and set it to expire
forceField.Parent = character
if duration then
task.delay(duration, function()
if forceField then
forceField:Destroy()
end
end)
end
end
end
end
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function(_character)
createCustomForcefield(player, FORCE_FIELD_DURATION)
end)
end
Players.PlayerAdded(onPlayerAdded)