Humanoid root part doesnt get detect after player dies

So I made this script to launch the player back after activating the tool but If the player dies then tries to use the tool again it doesn’t work and I just get the error that HumanoidRootPart is not a valid member of my character. Here is the script:

local tool = script.Parent

local Player = game.Players.LocalPlayer

if not Player.Character then
	Player.CharacterAdded:Wait()
end

local character = Player.Character

local humanoid = character:WaitForChild("Humanoid")


local function getModelMass(model: Model): number
	local mass = 0

	for _, v in ipairs(model:GetDescendants()) do
		if v:IsA('BasePart') then
			mass += v:GetMass()
		end
	end

	return mass
end

local backwardsForce = getModelMass(character) * 150 

tool.Activated:Connect(function()
	print("Activated")
	local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	print("Passed Humanoid")

	if HumanoidRootPart then
		print("HumanoidFound")
		humanoid.PlatformStand = true
		HumanoidRootPart:ApplyImpulse(-HumanoidRootPart.CFrame.LookVector.Unit * backwardsForce)
		print("Applied Pulse")
		wait(.1)
		tool.Parent.Humanoid.Health = 0
		print("Player Died")
	end
end)

I think this might be because the entire Character is respawned and loses it’s original value. So you need to redo your character variable in the tool.Activated:Connect section.

How Can I do this? I dont have any idea since I don’t know to much about scripting.

Also Happy DevForum aniversary

I kinda edited your script a little bit but it works

You want to add a remote event in ReplicatedStorage named ApplyForce and you want to add a script in ServerScriptService

Put this code in your Tool script:

local Player = game.Players.LocalPlayer

local tool = script.Parent

local activated = false

tool.Activated:Connect(function()
	if not activated then
		game.ReplicatedStorage.ApplyForce:FireServer(Player)
		activated = true
		wait(5)
		activated = false
	end
end)

Then put this code in your Server script:

local function getModelMass(model: Model): number
	local mass = 0

	for _, v in ipairs(model:GetDescendants()) do
		if v:IsA("BasePart") then
			mass += v:GetMass()
		end
	end

	return mass
end

game.ReplicatedStorage.ApplyForce.OnServerEvent:Connect(function(player)
	local char = game.Workspace:FindFirstChild(player.Name)
	
	local backwardsForce = getModelMass(char) * 150 
	
	if char.Humanoid and char.HumanoidRootPart then
		wait()
		char.Humanoid.PlatformStand = true
		local BodyForce = Instance.new("BodyForce",char.HumanoidRootPart)
		BodyForce.Force = Vector3.new(1 ,0, backwardsForce * 10)
		wait(0.1)
		char.Humanoid.Health = 0
	end
end)

I tried your code but it doesnt output any errors and it doesnt do anything.

Thanks for the anniversary message. I hadn’t noticed. I was so pleased when I finally got to register on here.

Check out @ testattempt idea and let us know if it works. Try changing the following:

tool.Activated:Connect(function()
	print("Activated")
	character = Player.Character --INSERT THIS IN YOUR SCRIPT
	local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	print("Passed Humanoid")
1 Like

Oh now it works! Thank you. Have a nice day/Night