After the player dies npc stops following any player any help?
local DISTANCE = 70
local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")
local origin = script.Parent.Head
local equip = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Equip)
local idle = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Idle)
local fire = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Fire)
local walk = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Walk)
walk:Play()
db = false
coroutine.wrap(function()
while task.wait() do
for _, player in pairs(Players:GetPlayers()) do
if player.Character then
local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {player.Character, workspace.Map}
rayParams.FilterType = Enum.RaycastFilterType.Whitelist
local result = workspace:Raycast(origin.Position, CFrame.lookAt(origin.Position, HumanoidRootPart.Position).LookVector * DISTANCE, rayParams)
if result and result.Instance:IsDescendantOf(player.Character) then
local name = player.Name
script.Parent.Head.face.Transparency = 0
idle:Play()
equip:Play()
local numberrandom = math.random(1,5)
if db == false then
db = true
fire:Play()
game.Workspace:WaitForChild(name):WaitForChild("Humanoid").Health -= 25
script.Shoot:Play()
wait(2)
db = false
end
script.Parent.Head.face.Transparency = 1
end
end
end
end
end)();
local RunService = game:GetService("RunService")
db = false
while wait() do
for _, player in pairs(Players:GetPlayers()) do
if player.Character then
local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {player.Character, workspace.Map}
rayParams.FilterType = Enum.RaycastFilterType.Whitelist
local result = workspace:Raycast(origin.Position, CFrame.lookAt(origin.Position, HumanoidRootPart.Position).LookVector * DISTANCE, rayParams)
if result and result.Instance:IsDescendantOf(player.Character) then
local char = game.Workspace:FindFirstChild(player.Name)
if char then
wait()
local name = player.Name
local ObjectPosition = game.Workspace:WaitForChild(name).PrimaryPart.Position
script.Parent.PrimaryPart.CFrame = CFrame.new(script.Parent.PrimaryPart.Position, Vector3.new(ObjectPosition.x, script.Parent.PrimaryPart.Position.y, ObjectPosition.z))
local function FollowPlayer()
local Path = PathfindingService:CreatePath()
local MovingToPosition = game.Workspace:WaitForChild(name):FindFirstChild("HumanoidRootPart").Position
local ComputedPath = Path:ComputeAsync(script.Parent.PrimaryPart.Position, MovingToPosition)
return Path
end
if char then
RunService.Stepped:Connect(function()
FollowPlayer()
local Path = FollowPlayer()
for _, waypoint in pairs(Path:GetWaypoints()) do
script.Parent.Humanoid:MoveTo(waypoint.Position)
end
end)
end
end
end
end
end
end