Humanoid.StateChanged not working after death

Hi guys, when my player dies, all the tools in the backpack are destroyed. When they respawn after death, my function fills up my player’s backpack with the tools they own.

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(playerCharacter)
        -- access the tool the player owns and
        local gearCloned = game.ServerStorage.Tool1:Clone()
        gearCloned.Parent = player.Backpack
    end)
end)

Within the tool is a LocalScript that detects humanoid.StateChanged.

image

--## PLAYER
local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")

--## LOCAL FUNCTIONS
humanoid.StateChanged:Connect(function(oldState, newState)
		-- if Character state is currently Jumping
		if newState == Enum.HumanoidStateType.Jumping then
			print(player.Name.." is Jumping")
		-- if Character state is currently FreeFalling
		elseif newState == Enum.HumanoidStateType.Freefall then
			print(player.Name.." is Gliding")
		-- if Character state is currently Landed	
		elseif newState == Enum.HumanoidStateType.Landed then
			print(player.Name.." is Landed")
		end
end)

When the script is cloned and parented to the player after the player respawns, it runs! But somehow the humanoid.StateChanged:Connect event doesn’t work anymore. Has anyone faced a similar issue before and how did you solve it?

1 Like

It is probably because the humanoid variable is set to a previous one, not the current one, it doesn’t sync with current one , you should do this instead:

--## PLAYER
local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function()
	if player.Character then
		local humanoid = player.Character:WaitForChild("Humanoid")
		humanoid.StateChanged:Connect(function(oldState, newState)
			-- if Character state is currently Jumping
			if newState == Enum.HumanoidStateType.Jumping then
				print(player.Name.." is Jumping")
				-- if Character state is currently FreeFalling
			elseif newState == Enum.HumanoidStateType.Freefall then
				print(player.Name.." is Gliding")
				-- if Character state is currently Landed	
			elseif newState == Enum.HumanoidStateType.Landed then
				print(player.Name.." is Landed")
			end
		end)
	end
end)

Should work well:

3 Likes