PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

BlastPressure

Explosion

float

Used to determine the amount of force applied to BaseParts caught in the Explosion/BlastRadius.

Currently this level of force applied does not vary based on distance from Explosion/Position. Unanchored BaseParts will accelerate equally away from the origin regardless of distance provided they are within the blast radius.

The blast pressure determines the acceleration of parts due to an explosion. It does not determine the degree to which joints are broken. When Explosion/DestroyJointRadiusPercent is equal to 1 all joints between parts in the Explosion/BlastRadius will be destroyed provided BlastPressure is greater than 0.

The BlastPressure also does not determine the amount of damage given to Terrain. Provided BlastPressure is greater than 0 and Explosion/ExplosionType isn’t set to Enum.ExplosionType.NoCraters the size of the crater created is determined exclusively by the Explosion/BlastRadius.

Setting BlastPressure to 0 eliminates the effect of the explosion and is useful when developers want to program their own custom behavior for explosions using the Explosion/Hit event.


Code Samples


Custom Explosion

This sample contains a simple function that creates a custom explosion. The custom explosion will not break joints as Explosion.DestroyJointRadiusPercent is equal to 0. This means Humanoids will not be instantly killed as their neck joints are broken. In this example explosion.BlastPressure is equal to zero so as not to apply force or damage terrain but this can be changed.

The Explosion.Hit event is used to listen for contact. Once contact has been made the code will look for the parent model of the BasePart hit by the explosion. If that model exists, hasn’t already been damaged and has a Humanoid in it damage will be applied based on distance from the explosion’s origin.

local function customExplosion(position, radius, maxDamage)
	local explosion = Instance.new("Explosion")
	explosion.BlastPressure = 0 -- this could be set higher to still apply velocity to parts
	explosion.DestroyJointRadiusPercent = 0 -- joints are safe
	explosion.BlastRadius = radius
	explosion.Position = position
	
	-- set up a table to track the models hit
	local modelsHit = {}

	-- listen for contact
	explosion.Hit:Connect(function(part, distance)
		local parentModel = part.Parent
		if parentModel then 
			-- check to see if this model has already been hit 
			if modelsHit[parentModel] then
				return
			end
			-- log this model as hit
			modelsHit[parentModel] = true

			-- look for a humanoid
			local humanoid = parentModel:FindFirstChild("Humanoid")
			if humanoid then
				local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
				distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
				humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields
			end	
		end
	end)

	explosion.Parent = game.Workspace
end