Enemy dies after killing player

When my enemy character kills the player, my enemy character instantly dies.
The health of the enemy is set to 0 instantly, and the joints of the enemy are not destroyed. Gameplay is provided.
It’s supposed to kill you, then live.

--body parts
local myHuman = script.Parent:WaitForChild("Monster")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
--animations
local eat = script.Parent:WaitForChild("Eat")
local eatAnim = myHuman:LoadAnimation(eat)
eatAnim.Priority = Enum.AnimationPriority.Action
--sounds
local grabSound = myRoot:WaitForChild("Eat")

function cutFromCenter(part, sizeToHalve)
	local size = part.Size
	local mulVec, newSize
	if sizeToHalve == "X" then
		mulVec = Vector3.new(size.X/2, 0, 0)
		newSize = Vector3.new(size.X/2, size.Y, size.Z)
	elseif sizeToHalve == "Y" then
		mulVec = Vector3.new(0, size.Y/2, 0)
		newSize = Vector3.new(size.X, size.Y/2, size.Z)
	else mulVec = Vector3.new(0, 0, size.Z/2)
		newSize = Vector3.new(size.X, size.Y, size.Z/2)
	end
	local cf = part.CFrame
	part.CanTouch = false
	part.Position = cf*mulVec
	part.Size = newSize
	part.Anchored = false
	local clone = part:Clone()
	clone.Position = cf*(-mulVec)
	clone.Parent = part.Parent
	game:GetService("Debris"):AddItem(part,10)
	game:GetService("Debris"):AddItem(clone,10)
	return part, clone
end

function checkSight(target)
	local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 40000000)
	local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
	if hit then
		if hit:isDescendantOf(target.Parent) then
			task.wait(.05)
			return true
		end
	end
	return false
end

function findTarget()
	local target = nil
	local potTargets = {}
	local seeTargets = {}
	for i, v in ipairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("HumanoidRootPart")
		if human and torso then
			if human.Health > 0 then
				table.insert(potTargets, torso)
			end
		end
	end
	if #potTargets > 0 then
		for i, v in ipairs(potTargets) do
			if checkSight(v) then
				table.insert(seeTargets, v)
			elseif #seeTargets < 1 then
				target = v
			end
		end
	end
	if #seeTargets > 0 then
		for i,v in ipairs(seeTargets) do
			target = v
		end
	end
	return target
end


function died()
	task.wait(1)
	game:GetService("Debris"):AddItem(script.Parent,0.1)
end

myHuman.Died:Connect(died)

myRoot.Touched:Connect(function(obj)
	if not obj.Parent:FindFirstChild("Humanoid") and not obj.Parent.Parent:FindFirstChild("Humanoid") then
		cutFromCenter(obj)
	else
		local charchildren = obj.Parent:GetChildren()
		for i = 1, #charchildren do
			if charchildren[i]:IsA("MeshPart") or charchildren[i]:IsA("Part") then
				charchildren[i].CanTouch = false
				charchildren[i].CanCollide = false
				charchildren[i].CanQuery = false
			end
			if charchildren[i].Name == "HumanoidRootPart" then
				charchildren[i].Position = script.Parent.RightHand.Position
				local weld = Instance.new("WeldConstraint")
				weld.Part0 = script.Parent.RightHand
				weld.Part1 = charchildren[i]
				weld.Parent = script.Parent.RightHand
			end
			if charchildren[i]:IsA("Humanoid") then
				print(charchildren[i].Parent.Name.." "..charchildren[i].Name)
				charchildren[i].PlatformStand = true
				eatAnim:Play()
				eatAnim.Stopped:Wait()
				grabSound:Play()
				charchildren[i].Health -= 100000000
			end
		end
	end
end)

function main()
	local target = findTarget()
	if target then
		myHuman:MoveTo(target.Position)
	end
end

while task.wait(0.1) do
	if myHuman.Health < 1 then
		break
	end
	main()
end

1 Like

Try commenting out the part where the enemy deletes itself and see what happens.

This is because the enemy is welded to your character which causes it to also die when your character dies. You could try destroying the weld right before killing the player, or you could try using AlignPosition and AlignOrientation or some other constraints instead of a weld.

1 Like

Another solution is to break the weld before they die or change BreakJointsOnDeath to false.

I tried weld deletion, but it still happens. How would I do the align position and orientation thing?

Before killing the player, check to see if your player is about to die. If they are going to die when attacked by the enemy, process to kill the enemy instead. If the enemy has a script that does damage to the player, you can put this kind of code inside the enemy’s code.

When the enemy touches your character, create an AlignPosition and AlignOrientation and set the Attachment0 of both of them to an attachment in your character’s HumanoidRootPart and set the Attachment1 to an attachment in the enemy’s hand.

I’ve made this as an example and you can adjust it to fit your code

local rabbit = script.Parent
local root = rabbit.HumanoidRootPart

root.Touched:Connect(function(part)
	if not part.Parent:FindFirstChild("Humanoid") and not part.Parent.Parent:FindFirstChild("Humanoid") then
		cutFromCenter(part)
	else
		local Humanoid = part.Parent:FindFirstChild("Humanoid")
		local HumanoidRootPart = part.Parent:FindFirstChild("HumanoidRootPart")
		if not (Humanoid and HumanoidRootPart) then
			return
		end
		for i,v in part.Parent:GetChildren() do
			if v:IsA("BasePart") then
				v.CanTouch = false
				v.CanQuery = false
			end
		end
		
		Humanoid.PlatformStand = true

		local AlignPosition = Instance.new("AlignPosition")
		AlignPosition.RigidityEnabled = true
		AlignPosition.ReactionForceEnabled = false
		AlignPosition.Attachment0 = HumanoidRootPart:FindFirstChildOfClass("Attachment") -- supports both r15 and r6
		AlignPosition.Attachment1 = rabbit.RightHand.RightGripAttachment
		AlignPosition.Parent = rabbit.RightHand

		local AlignOrientation = Instance.new("AlignOrientation")
		AlignOrientation.RigidityEnabled = true
		AlignOrientation.ReactionTorqueEnabled = false
		AlignOrientation.Attachment0 = HumanoidRootPart:FindFirstChildOfClass("Attachment")
		AlignOrientation.Attachment1 = rabbit.RightHand.RightGripAttachment
		AlignOrientation.Parent = rabbit.RightHand
		
		eatAnim:Play()
		eatAnim.Stopped:Wait()
		grabSound:Play()
		Humanoid.Health = 0
		AlignPosition:Destroy()
		AlignOrientation:Destroy()
	end
end)

Also, setting CanCollide on your character’s body parts isn’t going to work and if that is an issue then you can use collision groups to make sure the enemy and your character can’t collide with each other.

1 Like