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

Hit

Fires when the Explosion hits a BasePart within its Explosion/BlastRadius. Returns the part hit along with the distance of the part from Explosion/Position.

Note that the effect of an Explosion is not disrupted by obstacles, this means parts shielded behind other parts will still be hit, even if the BasePart they are shielded behind is anchored.

This event will also fire when Explosion/BlastPressure is equal to zero. This means developers can program their own custom behavior for explosions by eliminating the explosion’s influence on BaseParts and Terrain.

Note that this event will fire for every BasePart hit. This means it can fire multiple times for the same player character (as the character Model is made up of multiple parts). For this reason when dealing custom damage using the Explosion.Hit event it’s recommended to implement a check to see if the character has already been hit by the Explosion.

Parameters

Name Type Default Description

The BasePart hit by the Explosion.

The distance of the hit from Explosion/Position.


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