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
if a single npc is just walking around, and you’re using a path finding function to direct the npc
this in and of itself isn’t really taxing on the server
now having a lot of npcs is a different kinda problem
in this case of having a lot of npcs, your path finding function isn’t being unoptimal, its the npc’s humanoids themselves (iirc)
if you’re trying to have a ton of npcs i recommend watching this video by suphi kaner: https://www.youtube.com/watch?v=UamdKI7k4Mw&t=21055s, its a journey but a very insightful one
what does this mean?
where does an event tie into this?
if you’re worried about just the server replicating the current position of the npc then there isn’t really much you can do
have you taken latency into account?