Death/Kill Sound Effect System Doesn't Work

Hello, so i’m trying to make a Death/Kill Sound Effect System like combat warriors, but it doesn’t work.

server script:


-- ServerScript to play sound on player's death
local remoteEvent = game.ReplicatedStorage:WaitForChild("PlaySoundEvent")

-- Function to play sound on player's death
local function onPlayerDeath(player, soundId)
	local char = game.Workspace:FindFirstChild(player.Name)
	if char then
		local torso = char:FindFirstChild("Torso") or char:FindFirstChild("UpperTorso")
		if torso then
			local sound = Instance.new("Sound")
			sound.SoundId = "rbxassetid://" .. soundId
			sound.Parent = torso
			sound.Loaded:Wait() -- Wait for the sound to load
			sound:Play()
		else
			warn("Torso not found for player: " .. player.Name)
		end
	else
		warn("Character not found for player: " .. player.Name)
	end
end

-- Event handler for the RemoteEvent
local function onPlaySoundReceived(player, soundId)
	player.CharacterAdded:Connect(function(char)
		local hum = char:FindFirstChild("Humanoid")
		if hum then
			hum.Died:Connect(function() onPlayerDeath(player, soundId) end)
		else
			warn("Humanoid not found for character: " .. char.Name)
		end
	end)
end

-- Connect the RemoteEvent to the handler function
remoteEvent.OnServerEvent:Connect(onPlaySoundReceived)

local script (in startergui) :

-- LocalScript to capture sound ID input from the TextBox
local textBox = script.Parent -- The TextBox for input
local remoteEvent = game.ReplicatedStorage:WaitForChild("PlaySoundEvent")

-- Function to handle the TextBox focus loss event
local function onTextBoxFocusLost(enterPressed)
	if enterPressed then
		local soundId = textBox.Text
		if soundId and tonumber(soundId) then
			-- Fire the RemoteEvent with the sound ID
			remoteEvent:FireServer(soundId)
		else
			warn("Invalid sound ID entered.")
		end
	end
end

-- Connect the TextBox focus loss event to the function
textBox.FocusLost:Connect(onTextBoxFocusLost)

please help!

ok first, instead of char = workspace:FindFirstChild(player.Name) you can just do char = player.Character

and i could be wrong but i dont think the humanoid is immediately there as soon as the character spawns so you should do

local hum = char:WaitForChild("Humanoid", 5) -- the 5 means it stops waiting after 5 seconds