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