Local Script stops working on death

I’m trying to create a R6 ragdoll script which activates once the characters health is in between 0 and 5.

Everything works fine until the player dies or respawns, I’ve tried redefining the character and humanoid after death using a Humanoid.Died event (which can be seen in the script down below), however it hasn’t worked.

I’m not really sure what the problem is here, it could be the initial humanoid.healthchanged event or it could be something else.

Local Script
local plr = game.Players.LocalPlayer

local humanoid

repeat wait()
	
until plr.Character

local char = plr.Character




local humanoid = char.Humanoid

local rag = game.ReplicatedStorage.RemoteEvents.rag
local unrag = game.ReplicatedStorage.RemoteEvents.unrag

local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()

local toggle = true

--------------- CHANGABLE VARIABLES
local health = 5 --- health at which ragdoll will happen
local wakeuptime = 10 --seconds before wakeup
local rep


--------------- SCRIPT



	
	
humanoid.Died:Connect(function()
	toggle = true
	Controls:Enable()

	if rep then
		rep:Disconnect()
	end
	
	char = plr.Character
	
	print(char)
	humanoid = char.Humanoid
	
end)



humanoid.HealthChanged:Connect(function(curhealth)
	
	if curhealth > 0 and curhealth <= health  then
	if toggle then
		toggle = false
			Controls:Disable()
			
		rag:FireServer(char,wakeuptime)
			print('Fired')
		
		rep = game:GetService("RunService").Stepped:Connect(function()
				humanoid.Sit = true
				
				for i,v in pairs(char:GetDescendants()) do
				if v:IsA('BasePart') and v.Name ~='Torso' then
					if v.Name ~='Torso' and v.Name ~= 'HumanoidRootPart' then
					v.CanCollide = true
						
					else
					
							v.CanCollide = false
							
							
						end
						
					end
					
				end
				
			end)
			
		end
		
		
		wait (wakeuptime)
		
		if toggle == false then
		toggle = true
		
		Controls:Enable()
		rep:Disconnect()
			unrag:FireServer()
			
		end
	end
end)



	
	

P.S - The Humanoid.Died event is just above the healthchanged event

Any support / help is appreciated as I just cant figure this out haha :slight_smile:

Fixed - ended up implementing a characteradded event instead of using humanoid.died, as setting the character to the character in the died event still includes the old character, whereas the characteradded is based on the new character spawning / loading in - so the char would be set to the new character (sorry I’m not very good at explaining, but I’m trying my best lol).

Hope this helps anybody who stumbles across it and is going through the same situation as I was.
Thanks for reading :slight_smile: