Explosion.Hit not detecting humanoid characters

I want to fix my script.Most of the script works, except when i generate and use explosions, they dont deal damage. I set DestroyJoint to 0 because i am making my own damage system, but when i test out the basic damage system, it works until it tries to detect the player, which it doesnt.Its a server script in workspace

What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I have looked for solutions on the forum, but none of them work, i also tried using print functions to see when it fails, and it fails when trying to detect the player

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- debugged script with verbose prints
local ShipFolder = workspace:FindFirstChild("Danzig") and workspace.Danzig:FindFirstChild("Ship")
local layers = game.ReplicatedStorage:FindFirstChild("Others") and game.ReplicatedStorage.Others:FindFirstChild("Layers")
local waypointfolder = ShipFolder and ShipFolder:FindFirstChild("Waypoints")
local ship = script.Parent
local artLocation = ship and ship.Parent and ship.Parent:FindFirstChild("ArtilleryLocation") and ship.Parent.ArtilleryLocation:GetChildren() or {}
local TweenService = game:GetService("TweenService")
local rng = Random.new()
local pos = workspace:FindFirstChild("Test")

-- Helper: find character model that contains a Humanoid (robust)
local function findCharacterModel(part)
	if not part then
		print("[findCharacterModel] given part is nil")
		return nil
	end

	-- try fast built-in: find first ancestor that is a Model
	local model = part:FindFirstAncestorWhichIsA("Model")
	if model then
		local h = model:FindFirstChildOfClass("Humanoid")
		if h then
			print("[findCharacterModel] found model via FindFirstAncestorWhichIsA:", model.Name)
			return model
		end
	end

	-- fallback: climb all parents until root (safer than fixed 5)
	local current = part.Parent
	local steps = 0
	while current do
		steps = steps + 1
		local h = current:FindFirstChildOfClass("Humanoid")
		if h then
			print(("[findCharacterModel] found humanoid at ancestor (steps=%d): %s"):format(steps, current:GetFullName()))
			return current
		end
		current = current.Parent
		if steps > 50 then -- safety
			break
		end
	end

	print("[findCharacterModel] no humanoid model found for part:", part.Name)
	return nil
end

local function customExplosion(position, radius, maxDamage)
	print(string.format("[customExplosion] creating explosion at %s radius=%s maxDamage=%s", tostring(position), tostring(radius), tostring(maxDamage)))
	local explosion = Instance.new("Explosion")
	explosion.BlastPressure = 0
	explosion.DestroyJointRadiusPercent = 1
	explosion.BlastRadius = radius
	explosion.Position = position

	local modelsHit = {}

	explosion.Hit:Connect(function(part, distance)
		if not part then
			print("[Explosion.Hit] part is nil")
			return
		end

		distance = distance or (position - (part.Position or position)).Magnitude
		print(("[Explosion.Hit] hit part=%s distance=%.2f"):format(part.Name, distance))

		local parentModel = findCharacterModel(part)
		if not parentModel then
			print("[Explosion.Hit] no parentModel (skipping)")
			return
		end

		if modelsHit[parentModel] then
			print("[Explosion.Hit] model already processed:", parentModel.Name)
			return
		end
		modelsHit[parentModel] = true

		local humanoid = parentModel:FindFirstChildOfClass("Humanoid")
		if not humanoid then
			print("[Explosion.Hit] no humanoid in", parentModel.Name)
			return
		end

		local distanceFactor = 1 - (distance / explosion.BlastRadius)
		if distanceFactor <= 0 then
			print("[Explosion.Hit] distanceFactor <= 0, no damage")
			return
		end

		local damage = maxDamage * distanceFactor
		print(("[Explosion.Hit] about to deal %.2f damage to %s (health before=%.2f)"):format(damage, parentModel.Name, humanoid.Health or 0))

		-- protect call in case Humanoid API changes
		local ok, err = pcall(function()
			humanoid:TakeDamage(damage)
		end)
		if not ok then
			warn("[Explosion.Hit] TakeDamage failed:", err)
		else
			print(("[Explosion.Hit] damage applied. New health approx: %.2f"):format(humanoid.Health or 0))
		end
	end)

	explosion.Parent = workspace

	explosion.AncestryChanged:Connect(function(_, newParent)
		if not newParent then
			explosion:Destroy()
		end
	end)
end

local function safeEnableClone(parentTarget, item)
	local clone = item:Clone()
	-- put in pcall when setting Enabled (some items don't have it)
	local ok, err = pcall(function()
		clone.Parent = parentTarget
		if clone:IsA("ParticleEmitter") or clone:IsA("Trail") or clone:IsA("Beam") then
			-- these have Enabled property
			clone.Enabled = true
		else
			-- set a property if exists (not erroring)
			if clone:GetAttribute() and clone:GetAttribute("Enabled") ~= nil then
				-- nothing standard here, just demonstration
			end
		end
	end)
	if not ok then
		warn("[safeEnableClone] failed to parent/enable clone:", err)
		clone.Parent = nil
	else
		game.Debris:AddItem(clone, 0.5)
	end
end

local function shoot()
	while true do
		task.wait(1)
		if rng:NextInteger(1, 2) == 1 then
			print("[shoot] shooting")
			local ball = Instance.new("Part")
			ball.Shape = Enum.PartType.Ball
			ball.Material = Enum.Material.Neon
			ball.BrickColor = BrickColor.new("New Yeller")
			ball.Size = Vector3.new(5, 5, 5)
			ball.Anchored = true
			ball.CanCollide = false
			ball.Parent = workspace

			local randgun = rng:NextInteger(1, 2)
			if randgun == 1 and ship:FindFirstChild("Gun1") then
				ball.Position = ship.Gun1.Position
			elseif ship:FindFirstChild("Gun2") then
				ball.Position = ship.Gun2.Position
			else
				print("[shoot] no gun parts found on ship, destroying ball")
				ball:Destroy()
				continue
			end

			 -- fallback; replace with artLocation if desired
			if not pos or not pos:IsA("BasePart") then
				print("[shoot] workspace.Test missing or not a BasePart. Using ball.Position as target pos.")
				pos = { Position = ball.Position } -- dummy table with Position field
			end

			print("[shoot] target pos at:", tostring(pos.Position))
			local upTween = TweenService:Create(ball, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = pos.Position + Vector3.new(0, 100, 0)})
			local downTween = TweenService:Create(ball, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = pos.Position})

			upTween:Play()
			upTween.Completed:Connect(function()
				print("[shoot] upTween completed")
				downTween:Play()
			end)

			downTween.Completed:Connect(function()
				print("[shoot] downTween completed; cloning effects and creating explosion")
				-- Clone & enable effects safely
				for _, v in pairs(script:GetChildren()) do
					-- ensure not cloning scripts (optional)
					if not v:IsA("Script") and not v:IsA("LocalScript") then
						safeEnableClone(pos, v)
					end
				end
				
				customExplosion(pos.Position, 20, 100)
				if pos then
					pos:Destroy()
				end
				ball:Destroy()
				print("[shoot] ball destroyed after explosion")
			end)
		end
	end
end

task.spawn(shoot)


2 Likes

(Assuming your characters are the default roblox avatars)
All relevant parts are shallow. This means you just need to check if a basepart’s Parent is a model and find the humanoid from there.

2 Likes

Are you getting any error messages ? Are your print statements working ? Have you done a print of print(humanoid) or print(h) or print(parentModel) to see what actually prints ? I am getting nil when in Insert print statements.

1 Like

Of course my setup is likely different than yours b/c I don’t know how your Ship is setup.

1 Like

What do you get when you do a print statement of current
print(current)

1 Like

In your findCharacterModel(part) function do this:
‘’’
local Players = game:GetService(“Players”)

for _, player in ipairs(Players:GetPlayers()) do
	local Character = player.Character
	if Character then
		local h = Character:FindFirstChild("Humanoid")
		if h then
			--print("Humanoid found for player:", player.Name, Humanoid)
			print("[findCharacterModel] found model via FindFirstAncestorWhichIsA:", Character.Name)
			return Character
		end
	end
end

‘’’

2 Likes

Yea the prints work until the find character model function where it stops printing

1 Like