I have my NPCs moving in a loop and my Goal is that they do this smoothly. Unfortunately they keep stuttering after a given amount of time. I thought that maybe the issue is the amount of times i call the loop so i lowered that but it still starts stuttering after some time.
for i, NPC in Active_NPCs do
print(NPC)
local NPChum = NPC.Humanoid
if NPC:GetAttribute("ChosenPath") == nil then
NPC:SetAttribute("ChosenPath",math.random(1,#Destinations))
end
if NPC:GetAttribute("Moving") == false and NPC:GetAttribute("MogBattle") == false then
warn("NPC CHOSEN")
task.spawn(function()
if NPC:GetAttribute("DestinationReached") then
for i = #Destinations[NPC:GetAttribute("ChosenPath")], 1, -1 do
local Waypoint = Destinations[NPC:GetAttribute("ChosenPath")][i]
NPC:SetAttribute("Moving",true)
NPChum:MoveTo(Waypoint.Position)
NPChum.MoveToFinished:Wait()
if i == 1 then
NPC:SetAttribute("DestinationReached",false)
NPC:SetAttribute("Moving",false)
NPC:SetAttribute("ChosenPath",math.random(1,#Destinations))
task.delay(.1,MoveNPC)
end
end
else
for i, Waypoint in ipairs(Destinations[NPC:GetAttribute("ChosenPath")]) do
NPC:SetAttribute("Moving",true)
print("Im moving")
local NPChum = NPC.Humanoid
NPChum:MoveTo(Waypoint.Position)
NPChum.MoveToFinished:Wait()
if i == #Destinations[NPC:GetAttribute("ChosenPath")] then
NPC:SetAttribute("DestinationReached",true)
NPC:SetAttribute("Moving",false)
task.delay(.1,MoveNPC)
end
end
end
end)
end
end
end