Script Not Clearing All Children Of Class

I’ve created a script to clone and then remove the particle, but the particle does not work.
Output: attempt to index nil with ‘GetChildren’

I can’t figure what what’s going wrong

script.Parent.Parent.MeleeDamage.OnServerEvent:Connect(function(player)
	local function TargetPlayer(hit)
		humanoid = hit.Parent:FindFirstChild("Humanoid")
		torso = hit.Parent:FindFirstChild("UpperTorso")
		local tag = Instance.new("ObjectValue")
		tag.Value = player
		tag.Name = "creator"
		tag.Parent = humanoid
		game:GetService("Debris"):AddItem(tag,5)
	end
	script.Parent.Parent.Parent.Hitbox.Touched:Connect(TargetPlayer)
	if humanoid then
		humanoid:TakeDamage(35)
		print("Damaged")
	end
	humanoid = nil
	script.Blood:Clone().Parent = torso
	wait(0.1)
	for i,v in pairs(torso:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v:Destroy()
			print("Destroyed")
		end
	end
end)

Anyways, thanks and stay safe!

The assigned torso is inside of a function thats why you cant configure it.

1 Like

The problem is with hit.Parent. It is not confirmed that only one Parent can be used. make an elseif statement saying elsif hit.Parent.Parent:FindFirstChild(“Humanoid”) then. Character = hit.Parent.parent.

Also second problem as @Sorbious says.

On top of that, the if humanoid statement could run before the touched function triggers, causing it to see that there is no humanoid and to just move on.

How would I prevent that? Do I implement an if statement?

I guess I would just add a wait(0.5) before that if statement, that way it gives the game enough time to check if it was touched.