範例程式碼
爆炸實例化
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- 傷害地形
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspaceAPI 參考
屬性
BlastPressure
範例程式碼
自訂爆炸
local function customExplosion(position, radius, maxDamage)
local explosion\ = Instance.new("Explosion")
explosion.BlastPressure = 0 -- 這可以設置更高,以便對零件仍然施加速度
explosion.DestroyJointRadiusPercent\ = 0 -- 關節是安全的
explosion.BlastRadius = radius
explosion.Position\ = position
-- 設置一個表來跟踪被擊中的模型
local modelsHit\ = {}
-- 監聽接觸事件
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- 檢查此模型是否已經被擊中
if modelsHit[parentModel] then
return
end
-- 記錄此模型為被擊中
modelsHit[parentModel]\ = true
-- 尋找 humanoid
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance /\ explosion.BlastRadius -- 獲取距離作為 0 到 1 之間的值
distanceFactor = 1 - distanceFactor -- 給數值反轉,以便距離越近傷害越高
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage 以尊重 ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox 會在幾秒鐘後移除爆炸,但不會銷毀它們。
-- 為了確保我們的 .Hit 連接被斷開,一旦它被移除,就銷毀爆炸。
explosion.AncestryChanged:Connect(function()
if not explosion.Parent\ then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0,\ 10, 0), 12, 50)BlastRadius
範例程式碼
爆炸實例化
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- 傷害地形
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspaceDestroyJointRadiusPercent
範例程式碼
非致命爆炸
local function onDescendantAdded(instance)
if instance:IsA("Explosion") then
local explosion = instance
explosion.DestroyJointRadiusPercent = 0
local destroyJointRadiusPercent = 1
explosion.Hit:Connect(function(part, distance)
-- 檢查該部件是否在可破壞關節的範圍內
if distance <= destroyJointRadiusPercent * explosion.BlastRadius then
-- 確保該部件不屬於角色
if not game.Players:GetPlayerFromCharacter(part.Parent) then
part:BreakJoints()
end
end
end)
end
end
workspace.DescendantAdded:Connect(onDescendantAdded)自訂爆炸
local function customExplosion(position, radius, maxDamage)
local explosion\ = Instance.new("Explosion")
explosion.BlastPressure = 0 -- 這可以設置更高,以便對零件仍然施加速度
explosion.DestroyJointRadiusPercent\ = 0 -- 關節是安全的
explosion.BlastRadius = radius
explosion.Position\ = position
-- 設置一個表來跟踪被擊中的模型
local modelsHit\ = {}
-- 監聽接觸事件
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- 檢查此模型是否已經被擊中
if modelsHit[parentModel] then
return
end
-- 記錄此模型為被擊中
modelsHit[parentModel]\ = true
-- 尋找 humanoid
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance /\ explosion.BlastRadius -- 獲取距離作為 0 到 1 之間的值
distanceFactor = 1 - distanceFactor -- 給數值反轉,以便距離越近傷害越高
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage 以尊重 ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox 會在幾秒鐘後移除爆炸,但不會銷毀它們。
-- 為了確保我們的 .Hit 連接被斷開,一旦它被移除,就銷毀爆炸。
explosion.AncestryChanged:Connect(function()
if not explosion.Parent\ then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0,\ 10, 0), 12, 50)ExplosionType
範例程式碼
停止爆炸對地形造成傷害
-- 當一個後裔被添加時的函數
local function onDescendantAdded(instance)
if instance:IsA("Explosion") then
-- 設置爆炸類型為無坑塭
instance.ExplosionType = Enum.ExplosionType.NoCraters
-- 當 ExplosionType 屬性變更時的連接
instance:GetPropertyChangedSignal("ExplosionType"):Connect(function()
instance.ExplosionType = Enum.ExplosionType.NoCraters
end)
end
end
-- 監聽工作區中的後裔添加事件
workspace.DescendantAdded:Connect(onDescendantAdded)Position
範例程式碼
爆炸實例化
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- 傷害地形
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspaceVisible
範例程式碼
爆炸自訂視覺
local function customExplosion(position)
local explosion = Instance.new("Explosion")
explosion.Position = position
explosion.Visible = false
local attachment = Instance.new("Attachment")
attachment.Position = position
attachment.Parent = workspace.Terrain
local particleEmitter = Instance.new("ParticleEmitter")
particleEmitter.Enabled = false
particleEmitter.Parent = attachment
particleEmitter.Speed = NumberRange.new(5, 30)
particleEmitter.SpreadAngle = Vector2.new(-90, 90)
explosion.Parent = workspace
particleEmitter:Emit(20)
task.delay(5, function()
if attachment then
attachment:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0))活動
Hit
範例程式碼
自訂爆炸
local function customExplosion(position, radius, maxDamage)
local explosion\ = Instance.new("Explosion")
explosion.BlastPressure = 0 -- 這可以設置更高,以便對零件仍然施加速度
explosion.DestroyJointRadiusPercent\ = 0 -- 關節是安全的
explosion.BlastRadius = radius
explosion.Position\ = position
-- 設置一個表來跟踪被擊中的模型
local modelsHit\ = {}
-- 監聽接觸事件
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- 檢查此模型是否已經被擊中
if modelsHit[parentModel] then
return
end
-- 記錄此模型為被擊中
modelsHit[parentModel]\ = true
-- 尋找 humanoid
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance /\ explosion.BlastRadius -- 獲取距離作為 0 到 1 之間的值
distanceFactor = 1 - distanceFactor -- 給數值反轉,以便距離越近傷害越高
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage 以尊重 ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox 會在幾秒鐘後移除爆炸,但不會銷毀它們。
-- 為了確保我們的 .Hit 連接被斷開,一旦它被移除,就銷毀爆炸。
explosion.AncestryChanged:Connect(function()
if not explosion.Parent\ then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0,\ 10, 0), 12, 50)