local NPC = script.Parent
local Home = Vector3.new(-152.5, 0.5, -78.5)
local PathFindingService = game:GetService("PathfindingService")
while true do wait() print("Running")
NPC.Man.WalkSpeed = 16.2
for i, v in ipairs(NPC:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") then
v:SetNetworkOwner()
v.CanCollide = false
end
end
local Check = workspace:GetPartsInPart(NPC.HumanoidRootPart)
for i, v in ipairs(Check) do
if v.Name == "HumanoidRootPart" or v.Parent:FindFirstChild("HumanoidRootPart") then
local Humanoid = v.Parent:FindFirstChild("Humanoid")
if Humanoid then
Humanoid:TakeDamage(33)
end
end
end
for i, v in ipairs(NPC:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") then
v:SetNetworkOwner()
v.CanCollide = false
end
end
local Players = game.Players:GetPlayers()
local Target, Distance = nil, math.huge
for i, v in ipairs(Players) do
if v.Character then
local Character = v.Character
local Hum = Character:FindFirstChild("Humanoid")
if Hum then
local Health = Hum.Health
if Health then
if Health > 0 then
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootpart")
local Magnitude = (NPC.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude
if Magnitude < Distance then
Distance = Magnitude
Target = Character.HumanoidRootPart
end
else
print("Under")
end
else
warn("No Health")
end
else
warn("No Hum")
end
end
end
local Humanoid = nil
if Target then
Humanoid = Target.Parent:FindFirstChild("Humanoid")
NPC.Man.WalkSpeed = 16.2
if Humanoid then
local Health = Target.Parent.Humanoid.Health
local Direction, RayCast, Result
Direction = (Target.Position - NPC.HumanoidRootPart.Position).Unit * 1000
RayCast = Ray.new(NPC.HumanoidRootPart.Position, Direction)
Result = workspace:Raycast(NPC.HumanoidRootPart.Position, Direction)
if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
if Health and Health > 0 then
NPC.Man:MoveTo(Target.Position)
NPC.Man.WalkSpeed = 16.2
else
NPC.Man:MoveTo(Home)
NPC.Man.WalkSpeed = 69
end
elseif Humanoid then
local Path = PathFindingService:CreatePath()
Path:ComputeAsync(NPC.HumanoidRootPart.Position, Target.Position)
local Vpos
for i, v in ipairs(Path:GetWaypoints()) do
local Part
Part = Instance.new("Part")
Part.Position = v.Position
Part.Anchored = true
Part.Shape = Enum.PartType.Ball
Part.CanCollide = false
--Part.Parent = workspace.Trash
if i == #Path:GetWaypoints() then
Vpos = v.Position
end
end
local TargetPos
for i, v in ipairs(Path:GetWaypoints()) do
for i, v in ipairs(Players) do
if v.Character then
local Character = v.Character
local Hum = Character:FindFirstChild("Humanoid")
if Hum then
local Health = Hum.Health
if Health then
if Health > 0 then
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootpart")
local Magnitude = (NPC.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude
if Magnitude < Distance then
Distance = Magnitude
Target = Character.HumanoidRootPart
TargetPos = Target.Position
end
end
end
end
end
end
local TargetLength = (NPC.HumanoidRootPart.Position - Target.Position).Magnitude
local Magnitude = (Vpos - Target.Position).Magnitude
if Health > 0 then
NPC.Man:MoveTo(v.Position)
NPC.Man.WalkSpeed = 16.2
if TargetLength < Magnitude then
print("Closer")
NPC.Man:MoveTo(Target.Position)
for i, v in ipairs(NPC:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") then
v:SetNetworkOwner()
v.CanCollide = false
end
end
else
NPC.Man.MoveToFinished:Wait()
--PROBLEM
print(TargetPos)
print("Farther", TargetLength, Target.Position, Target)
for i, v in ipairs(NPC:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") then
v:SetNetworkOwner()
v.CanCollide = false
end
end
end
if i == #Path:GetWaypoints() then
for I, V in ipairs(workspace.Trash:GetChildren()) do
if V.Parent == workspace.Trash then
V:Destroy()
end
end
end
else
NPC.Man:MoveTo(Home)
NPC.Man.WalkSpeed = 69
end
end
end
else
Humanoid = nil
warn("Humanoid is nil")
NPC.Man:MoveTo(Home)
NPC.Man.WalkSpeed = 69
end
else
warn("Target is nil")
NPC.Man:MoveTo(Home)
NPC.Man.WalkSpeed = 69
end
end
After hours of thinking, occurring, and fixing problems, I think I’ve completed this script. I’ve made 4 different versions, the past 3 had problems. I still think that my script has problems but I don’t know why, where, or how, and I don’t even know if it has problems. I would like some suggestions to make it better.