Script not detecting player death

Just a couple hours ago, I’ve had this massive issue with my script that popped up. Scripts in my game that detect players death have about a 50/50 chance of actually detecting it. Half the time it wont connect, half the time it will. The 2 scripts are as basic as it gets, its a basic ragdoll script that you can get off of the toolbox and a sound on death script. The ragdoll script is probably too big to post so i’ll just do the sound one since they both don’t feel like working anyways.

function Died(p)
		wait(.001)
	local tracks=script:GetChildren()
	local rn=math.random(1,#tracks)
	local track=tracks[rn]
	if track~=nil then
		track:play()
	end
end

function Hum(p) 
	hum = p:findFirstChild("Humanoid") 
	if hum ~= nil then 
		print("Player Died") -- NOT PRINTING
		hum.Died:connect(function(hum) Died(p) end) 
	end 
end 

function Enter(p) 
	p.CharacterAdded:connect(Hum) 
end 
game.Players.PlayerAdded:connect(Enter)

If anyone can help me with this it will be greatly appreciated.

This should be the problem try to remove this line and see what happens.

Try using WaitForChild and see if that might be it, the humanoid might not be detected because by some chance it might not have loaded in fully:

hum = p:WaitForChild("Humanoid",3)

This didn’t work. Still having the same issue

Tried changing this. Still didn’t work.

Can you please try to print in every event so we know where it stops?

There is possibility that something is not firing correctly. And please use upper cased C in Connect :D.

I have an idea try to connect it to PlayerAdded instead of CharacterAdded event.

Try a re-arrangement. Maybe the character somehow already existed before player?

local Players = game:GetService("Players")

local Tracks = script:GetChildren() -- huh

local function onCharacterAdded(character)
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		print("Signal routed")
		humanoid.Died:Connect(function()
			local randomNumber = math.random(#Tracks)
			local track = Tracks[randomNumber]
			if track then
				track:Play()
			end
		end) -- if it errored, this was missing before
	end
end

local function onPlayerAdded(player)
	if player.Character then
		onCharacterAdded(player.Character)
	end
	
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Maybe it is because the player has joined, but their Character/Humanoid has yet to load in. Try giving it at least 1 second delay before connecting the function.

Why are you tried to use wait() then print

function Died(p)
	print("Player Died") -- PRINT FIRST
	wait(.001) -- NOTICE THERE IS A WAIT(.001)
	local tracks=script:GetChildren()
	local rn=math.random(1,#tracks)
	local track=tracks[rn]
	if track~=nil then
		track:play()
	end
end

function Hum(p) 
	hum = p:findFirstChild("Humanoid") 
	if hum ~= nil then 
		hum.Died:connect(function(hum) Died(p) end) 
	end 
end 

function Enter(p) 
	p.CharacterAdded:connect(Hum) 
end 
game.Players.PlayerAdded:connect(Enter)