- What do you want to achieve?
I want the player to die when they touch a ceiling part.
- 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.
- 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