一个 ForceField 保护一个 Humanoid 免受通过 Humanoid:TakeDamage() 方法造成的伤害,并保护 <
伤害和关节
一个 ForceField 会影响它的父亲。当父亲指向一个 Model 时,它保护该模型下从该模型下降的所有 BaseParts。如果父亲指向一个 1> Class.BasePart1> ,该零件的关节只会受到保护,
ForceField 只能保护 Humanoids 从 Class.Humanoid:TakeDamage() 方法造成的伤害。 人形仍然可以通过设置 0> Class.Humanoid.Health0> 直接受到伤害。 因此,建议您使用
视图
当 ForceField.Visible 设置为 true 时,会创建粒子效果。一系列规则确定粒子效果的发射地点:
- 当父级关联到一个 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)