Asigning the humanoid not working

Hello.

I’m trying to get the player’s humanoid for my script with a single variable, but it won’t work. Here’s what I did…

local Player = game:GetService("Players").LocalPlayer;
local Character = Player.Character or Player.CharacterAdded:Wait();
local Humanoid = Character.Humanoid or Character:FindFirstChild("Humanoid") or Character:WaitForChild("Humanoid");

Humanoid.Died:Connect(function()
	print("Died.");
end);

Here’s the output…

Humanoid is not a valid member of Model "Workspace.HATETOALL"

I literally added Character:WaitForChild(), Character:FindFirstChild() and Character.Humanoid all three in one variable, yet it doesn’t seem to work.
(Running this through a localscript in StarterPlayerScripts)

1 Like

Try this:

local Humanoid = Character:WaitForChild("Humanoid");
1 Like

That worked, but the Humanoid.Died connection disconnects afterwards. Any way to prevent that or fix that?

Keep in mind that whenever an Instance is removed all its connections are manually disconnected, however your script also resets and runs again so the connection is recreated every time the script is recreated.

1 Like