NPCs in my game have a schedule they follow, when certain times have been reached in-game, the NPC moves on to its next target destination, like at night they may return home.
The current way I’m doing it is looping through every single individual NPC in the same chunk the player is in. Obviously, this isn’t ideal for large numbers of NPCs. https://gyazo.com/d90ab3e32a3f8ba44afe64323f3e67d1
(In this video, all the NPCs have the same schedule but they’re all moving at drastically different times).
So I’m wondering how I could deal with this problem.
Additionally, I was wondering if the pathfinding should be done on the client or server side, I’m thinking my game is going to be single-player but I’m not sure if setting every NPC network ownership to the client will be a good idea.
Server script, NPC_controller
-- npc schedule, 24 hour time
local RoleTasks = {
["priest"] = {
["6"] = "Altar",
["11"] = "Sweep",
["12"] = "Library",
["15"] = "Altar",
["18"] = "Sweep",
["20"] = "Altar",
["22"] = "Bed"
}
}
-- When the clock time changes (every 0.5 seconds on the server), loop through all npcs within the player chunk
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local time = math.round(Lighting.ClockTime * 100)/100
if activeChunk then
for _, npc in pairs(CollectionService:GetTagged(activeChunk.Name)) do
local role = npc:GetAttribute("role")
local targetDestination = RoleTasks[role][tostring(time)] -- check npc schedule and where they should be at the current time
if targetDestination then
-- npc, target
path = NPC_Pathfinding.new(npc, workspace:FindFirstChild(activeChunk.Name):FindFirstChild(npc.Name):FindFirstChild(targetDestination).Position )
path:walk()
end
end
end
end)
if you want all of them to be at the same time i would recommend some form of
task.spawn()
however with that amount of ai using task.spawn() for each individual be likely very laggy, i instead would do something along the lines of
local Table1 = {}
local Table2 = {}
local Table3 = {}
task.spawn(function()
for i,v in pairs(Table1) do
path = NPC_Pathfinding.new(npc, workspace:FindFirstChild(activeChunk.Name):FindFirstChild(npc.Name):FindFirstChild(targetDestination).Position )
path:walk()
end
end)
task.spawn(function()
for i,v in pairs(Table2) do
path = NPC_Pathfinding.new(npc, workspace:FindFirstChild(activeChunk.Name):FindFirstChild(npc.Name):FindFirstChild(targetDestination).Position )
path:walk()
end
end)
task.spawn(function()
for i,v in pairs(Table3) do
path = NPC_Pathfinding.new(npc, workspace:FindFirstChild(activeChunk.Name):FindFirstChild(npc.Name):FindFirstChild(targetDestination).Position )
path:walk()
end
end)
where you split your table of ai into 3 or 5 and do task.spawn() for each table
as to your other question, it really matters what works in this case.
If your game was multiplayer network ownership would matter more as others players troops could be destroyed for no reason, but in the case of a single player game a exploiter only really cheats themselves.
it just matters if giving the client network owner ship improves the performance of the game.
You have the dev console, which can tell you the amount of memory your game is using on client and server. (be warned studio memory usage can be much higher then actual ingame memory.)
The normal performance tab is also very precise, letting you see the fps and heart beat of workspace on both client and server. As well as a bunch of other things i dont know about.