Player humanoid won't die

  1. What do you want to achieve?

I want the player to die when they touch a ceiling part.

  1. What is the issue?

No matter what I do, I cannot get the script to kill the player. The function is firing and the humanoid health is getting set to 0; yet he doesn’t die.

  1. What solutions have you tried so far?

TakeDamage() wasn’t working either. The destroyCharacter() function I created inconsistently kills when destroying the root part. I can see in the output and through the explorer that the health is hitting 0. When I manually input 0 myself it works.

Here’s the two functions I use to kill the player and destroy the model

-- Removes the player character model
local function destroyCharacter(rootPart)
	
	local character = rootPart.Parent
	rootPart.Anchored = true
	print("Destroy character")
	-- Loop through every instance in the character
	for _, v in pairs(character:GetChildren()) do
		
		-- Destroy everything except the Humanoid root part
		if v:IsA("MeshPart") or v:IsA("Part") or v:IsA("Accessory") then -- DESTROYS ROOTPART
			
			v:Destroy()
			
		end
	end
end

-- Kills the player when called; calls to remove model; calls for special effects; sets humanoid health to 0
local function destroyPlayer( hit )
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") or nil
	local rootPart = humanoid.Parent.HumanoidRootPart
	
	if humanoid and humanoid.Health > 0 and not debounce then
		
		print("PLAYER TOUCHED CEILING")
		
		debounce = true
		
		print(humanoid.Health)
		
		-- Play effects
		sfxRemote:FireAllClients(deathSound, ceiling)
		effectsRemote:FireAllClients("CeilingFlash", ceiling)
		
		-- Kill Character
		humanoid.Health = 0
		
		print(humanoid.Health)
		
		-- Remove player character
		destroyCharacter(rootPart)
		
		wait()
		
		debounce = false
		
	end
	
end
3 Likes

function DestroyCharacter(Character : Model?)
	if not Character then
		return
	end
	--[[
		Character:BreakJoints()
	]]
	Character:Destroy()
	Character = nil
end

function KillPlayer(Player: Player?)
	if not Player then
		return
	end
	if not Player.Character then
		return
	end
	local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
	
	Humanoid:ChangeState(Enum.HumanoidStateType.Dead)
	
	task.delay(task.wait(), function()
		Humanoid:TakeDamage(math.huge)
		task.wait()
		if Humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
			DestroyCharacter(Player.Character)
		end
	end)
end

The native Health made by roblox has a flawed bug to keep regening even when dead, you can fix by editing the health regen to while Humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
Never compare humanoid health to equal or less than 0 because of this case.

2 Likes

That worked beautifully. Thank you very much!

Edit: Quick update, had an issue like this again for a separate game. Original solution didn’t work, made some changes to the solved code…

task.delay(task.wait(), function()

		humanoid:TakeDamage(math.huge)
		humanoid:ChangeState(Enum.HumanoidStateType.Dead) -- Moved this inside the coroutine
        -- Didn't need to destroy the character for this game
	end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.