So, I just made an NPC that roams the map, and I wanted to know if I could do this better. the main thing I want help with is this:
local List = {}
for _, Part in pairs(script.Paths.Value:GetChildren()) do
if Part:IsA("BasePart") and (Part.Position - humanoid.RootPart.Position).Magnitude < 200 then
table.insert(List,Part)
end
end
if #List > 0 then
CurrentDestination = List[math.random(1,#List)]
else
warn("an NPC failed to find a path")
Character:Destroy()
end
there are about 200 hundred locations. all they do is simply walk to it.
here is the code for walking to it:
while wait() do
local target = findTarget()
CDMGCD += .1
if humanoid.Health == 0 then
break
end
if target and GlobalValues.IsDay.Value == false then
humanoid.WalkSpeed = 16
followPath(target.HumanoidRootPart.Position)
else
humanoid.WalkSpeed = 10
if CurrentDestination == nil then
GetCurrentDestination()
end
if (CurrentDestination.Position - humanoid.RootPart.Position).Magnitude < 5 then
GetCurrentDestination()
end
followPath(CurrentDestination.Position)
end
end
local target = findTarget()
CDMGCD += .1
LastHit += .1
SinceGoodPath -= .1
if humanoid.Health == 0 then
break
end
if target and GlobalValues.IsDay.Value == false then
humanoid.WalkSpeed = 16
followPath(target.HumanoidRootPart.Position)
else
humanoid.WalkSpeed = 10
if CurrentDestination == nil then
GetCurrentDestination()
end
if (CurrentDestination.Position - humanoid.RootPart.Position).Magnitude < 5 or SinceGoodPath <= 0 then
GetCurrentDestination()
SinceGoodPath = 20
end
local p = followPath(CurrentDestination.Position)
if p == false then
print("Forced Move")
GetCurrentDestination()
followPath(CurrentDestination.Position)
end
end
if LastHit > 5 then
nextWaypointIndex += 1
LastHit = 0
print("Hit Reset")
end
I’m having some big issues right now with them getting stuck