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

DestroyJointRadiusPercent

Explosion

float

Used to set the proportion of the Explosion/BlastRadius, between 0 and 1, within which all joints will be destroyed. Anything outside of this range will only have the Explosion force applied to it.

For example, if Explosion/BlastRadius is set to 100 and DestroyJointRadiusPercent is set to 0.5, any joints within a radius of 50 studs would be broken. Any joints between the ranges of 50 and 100 studs wouldn’t be destroyed, but the Explosion's force would still be applied to the BaseParts.

This property allows developers to make Explosions ‘non-lethal’ to Humanoids by setting DestroyJointRadiusPercent to 0. This means the neck joint will not be broken when characters come into contact with the Explosion.


Code Samples


Non lethal explosions

This sample includes an example of how Explosions can be made non lethal to player characters.

-- listen for new Explosion's being added to the workspace
workspace.DescendantAdded:Connect(function(instance)
	if instance:IsA("Explosion") then
		local explosion = instance
		-- set DestroyJointRadiusPercent to 0
		explosion.DestroyJointRadiusPercent = 0
		-- implement our own DestroyJointRadiusPercent
		local destroyJointRadiusPercent = 1
		explosion.Hit:Connect(function(part, distance)
			-- check the part is in range to break joints
			if distance <= destroyJointRadiusPercent * explosion.BlastRadius then
				-- make sure the part does not belong to a character
				if not game.Players:GetPlayerFromCharacter(part.Parent) then
					-- break joints
					part:BreakJoints()
				end
			end
		end)
	end
end)

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