Explosion.hit not working

ive been trying to make a granade launcher whit a really low velocity bullet and ive came into a problem, it doesnt seem like the explosion.hit event is working at all

ive looked into the dev hub and couldnt find anything useful about it so i decided to ask for help

current code:

game.ReplicatedStorage.Events.FireGL.OnServerEvent:Connect(function(player, MuzzlePos, mousePos, damage, limbDMG, CF, muzvel, cam)
	
	GLbullet = effects.GranadeShell:Clone()
	print("fired")
	local sound = player.Character.Torso:FindFirstChild("GLFIRE")
	local GLbullet2 = effects.GranadeShell:Clone()
	if sound then

		sound:Play()

	end
	GLbullet.Parent = game.Workspace.Munitions
	local origin = MuzzlePos
	local direction = (mousePos - origin)
	GLbullet.Position = MuzzlePos
GLbullet.Velocity = cam.LookVector * muzvel

	
local exploded = false
local function customExplosion(position, radius, maxDamage)
	local explosion = Instance.new("Explosion")
	explosion.BlastPressure = 0 
	explosion.DestroyJointRadiusPercent = 0 
	explosion.BlastRadius = radius
	explosion.Position = GLbullet.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

			
			local humanoid = parentModel:FindFirstChild("Humanoid")
			if humanoid then
					if player.Team or game:GetService("Players"):GetPlayerFromCharacter() == nil then
				local distanceFactor = distance / explosion.BlastRadius 
				distanceFactor = 1 - distanceFactor 
				humanoid:TakeDamage(maxDamage * distanceFactor) 
					elseif player.Team and game:GetService("Players"):GetPlayerFromCharacter(parentModel) and game:GetService("Players"):GetPlayerFromCharacter(parentModel).Team ~= player.Team then
						
						local distanceFactor = distance / explosion.BlastRadius 
						distanceFactor = 1 - distanceFactor
						humanoid:TakeDamage(maxDamage * distanceFactor) 
					end
			end
		end
	end)

	explosion.Parent = game.Workspace

	explosion.AncestryChanged:Connect(function()
		if not explosion.Parent then
			explosion:Destroy()
		end
	end)
end

	
GLbullet.Touched:Connect(function(hit)
	if exploded ~= true then
		if not GLbullet.Velocity then
		wait(0.1)
		customExplosion(Vector3.new(GLbullet.Position), 15, 100)
		GLbullet:Destroy()
		end
	end
end)
end)	

robloxapp-20241029-2145565.wmv (2.2 MB)
heres the video if anyone asks, and yes ive tried playing it whit other people and it still dint work, ive also tried deleting the GetPlayerFromCharacter() stuff and dint work still

So, both the if and the elseif have the same code block,
just thought you could somehow merge that

ALSO, you forgot to parent the explosion to workspace

Change that to this

local explosion = Instance.new("Explosion", workspace)

And all your problems are solved

about these the first one is for when for example a rig is hit its for testing porpuses, the second one is anti team kill i couldnt fit everything together in one block

ive parented the explosion to workspace and its still not working somehow, ive set up prints to see whats wrong and apparently the hit function appears not to be working

The second code block will never fire because it says player.Team and, and the first says player.Team or
Can I see your new code (since it is now parented to workspace)

local exploded = false
local function customExplosion(position, radius, maxDamage)
	local explosion = Instance.new("Explosion", workspace)
	explosion.BlastPressure = 0 
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = radius
	explosion.Position = GLbullet.Position
	

	
	local modelsHit = {}

	
	explosion.Hit:Connect(function(part, distance)
		print("hit")
		local parentModel = part.Parent
		if parentModel then
			
			if modelsHit[parentModel] then
				return
			end
			
			modelsHit[parentModel] = true

			
			local humanoid = parentModel:FindFirstChild("Humanoid")
			if humanoid then
					if player.Team or game:GetService("Players"):GetPlayerFromCharacter() == nil then
				local distanceFactor = distance / explosion.BlastRadius 
				distanceFactor = 1 - distanceFactor 
				humanoid:TakeDamage(maxDamage * distanceFactor) 
					elseif player.Team and game:GetService("Players"):GetPlayerFromCharacter(parentModel) and game:GetService("Players"):GetPlayerFromCharacter(parentModel).Team ~= player.Team then
						
						local distanceFactor = distance / explosion.BlastRadius 
						distanceFactor = 1 - distanceFactor
						humanoid:TakeDamage(maxDamage * distanceFactor) 
					end
			end
		end
	end)

	explosion.Parent = game.Workspace

	explosion.AncestryChanged:Connect(function()
		if not explosion.Parent then
			explosion:Destroy()
		end
	end)
end

	
GLbullet.Touched:Connect(function(hit)
	if exploded ~= true then
		if not GLbullet.Velocity then
		wait(0.1)
		customExplosion(Vector3.new(GLbullet.Position), 15, 100)
		GLbullet:Destroy()
		end
	end
end)
end)	

here it is

ive used the print() function and it seems like the .hit event is not firing as nothing comes out in the output edit: heres the output
image
the “fired” prints are when the grande launcher fires and theres supposed to be a “hit” when the explosion hits anything, nothing comes out

Are you sure that the customExplosion() function is being called?

yes, ive made a .touched event on the shell later on to detect when the bullet is it, i wont use fasct cast because its a really low velocity bullet as i said

GLbullet.Touched:Connect(function(hit)
	if exploded ~= true then
		if not GLbullet.Velocity then
		wait(0.1)
		customExplosion(Vector3.new(GLbullet.Position), 15, 100)
		GLbullet:Destroy()
		end
	end
end)
end)	

i have the velocity detection to make sure it actually hit something otherwise it would explode the other shells that are mid air, if it dint fire i wouldnt see the explosion graphics beacuse ive done them in a seperate script that affects all explosion instances

Try to put a print call in the very beggining of customExplosion function and look if it prints anything

image
odd,
image
it creates the explosion but doesnt do anything

Does the explosion happen visually?

Try to change:

explosion.BlastRadius = radius

to:

explosion.BlastRadius = 500

and see if it hits something

I suppose the problem is radius that u apply to the explosion

yep but i think i figured it out, it was the “if velocity was interfering”

local exploded = false
local function customExplosion(position, radius, maxDamage)
	print("exploded")
	local explosion = Instance.new("Explosion", workspace)
	explosion.BlastPressure = 0 
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = radius
	explosion.Position = GLbullet.Position
	

	
	local modelsHit = {}

	
	explosion.Hit:Connect(function(part, distance)

		local parentModel = part:FindFirstAncestorWhichIsA("Model")
		if parentModel then
			
			if modelsHit[parentModel] then
				return
			end
			
			modelsHit[parentModel] = true

			
			local humanoid = parentModel:FindFirstChild("Humanoid")
			if humanoid then
					if game:GetService("Players"):GetPlayerFromCharacter(parentModel) == nil then
				local distanceFactor = distance / explosion.BlastRadius 
				distanceFactor = 1 - distanceFactor 
				humanoid:TakeDamage(maxDamage * distanceFactor) 
					elseif player.Team and game:GetService("Players"):GetPlayerFromCharacter(parentModel) and game:GetService("Players"):GetPlayerFromCharacter(parentModel).Team ~= player.Team then
						
						local distanceFactor = distance / explosion.BlastRadius 
						distanceFactor = 1 - distanceFactor
						humanoid:TakeDamage(maxDamage * distanceFactor) 
					end
			end
		end
	end)

	explosion.Parent = game.Workspace

	explosion.AncestryChanged:Connect(function()
		if not explosion.Parent then
			explosion:Destroy()
		end
	end)
end

	
GLbullet.Touched:Connect(function(hit)
	if exploded ~= true then
		
		customExplosion(Vector3.new(GLbullet.Position), 15, 100)
		
	end
end)
end)	

this is my fix, at the cost of the granade shells exploding mid air

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.