I’m trying to make a custom entity that damages the player.
local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local myHead = script.Parent:WaitForChild("Head")
local hurt = false
local oldHealth = myHuman.Health
local pathCount = 0
local dead = false
local closestplr
local idle = myHuman:LoadAnimation(myHuman.Idle)
local walk = myHuman:LoadAnimation(myHuman.Walk)
local run = myHuman:LoadAnimation(myHuman.Run)
local clone = script.Parent:Clone()
local function getclosestplayer()
local faceling_Pos = myRoot.Position
local distance = math.huge
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") then
if game.Players:FindFirstChild(player.Name) then
local playerpos = player.HumanoidRootPart.Position
local distancefrombot = (faceling_Pos - playerpos).magnitude
if distancefrombot <= 30 then
if distancefrombot < distance then
distance = distancefrombot
closestplr = player
if distancefrombot <= 5 then
hurt = true
end
end
else
closestplr = nil
end
end
end
end
return closestplr
end
function getUnStuck()
myHuman:Move(Vector3.new(math.random(-1,-1),0,math.random(-1,-1)))
myHuman.Jump = true
wait(0.5)
end
function pathToLocation(location)
getclosestplayer()
if closestplr == nil then
idle:Stop()
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position, location)
local wayPoints = path:GetWaypoints()
pathCount = pathCount + 1
myRoot:SetNetworkOwner(nil)
if path.Status == Enum.PathStatus.Success then
local currentPathCount = pathCount
getclosestplayer()
for i, waypoint in pairs(wayPoints) do
if currentPathCount ~= pathCount then
return
end
if waypoint.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
myHuman:MoveTo(waypoint.Position)
myRoot.footsteps:Play()
walk:Play()
getclosestplayer()
if closestplr == nil then
delay(0.5, function()
if myHuman.WalkToPoint.Y > myRoot.Position.Y then
myHuman.Jump = true
end
end)
local moveSuccess = myHuman.MoveToFinished:Wait()
walk:Stop()
if not moveSuccess then
break
end
end
end
else
getUnStuck()
end
end
end
function wander()
local randX = math.random(-100,100)
local randZ = math.random(-100,100)
local goal = myRoot.Position + Vector3.new(randX,0,randZ)
pathToLocation(goal)
end
myHuman.Died:Connect(function()
dead = true
wait(15)
clone.Parent = workspace
game:GetService("Debris"):AddItem(script.Parent,0.1)
end)
while wait() do
hurt = false
getclosestplayer()
if closestplr == nil then
if myHuman.Health > 0 then
myHuman.WalkSpeed = 9
getclosestplayer()
wander()
idle:Play()
getclosestplayer()
myRoot.footsteps:Stop()
wait(math.random(1,6))
else
break
end
else
while closestplr ~= nil do
if closestplr.Humanoid.Health > 0 then
myHuman:MoveTo(closestplr.HumanoidRootPart.Position)
myHuman.WalkSpeed = 22
if hurt == true then
closestplr.Humanoid:TakeDamage(50)
if closestplr.Humanoid.Health <= 0 then
closestplr = nil
end
end
myRoot.footsteps.PlaybackSpeed = 2
myRoot.footsteps:Play()
run:Play()
myHuman.MoveToFinished:Wait()
getclosestplayer()
if closestplr == nil then
myHuman.WalkSpeed = 9
myRoot.footsteps:Stop()
myRoot.footsteps.PlaybackSpeed = 1
break
end
end
end
end
myRoot.footsteps:Stop()
end
The game crashes when the player dies.
I tried setting the closestplayer to nil so the entity doesn’t follow me when I die, but it didn’t work.
Can someone help me?