Hello, my game has a grid system, where the entire map is split into 5 by 5 stud blocks (with no unique identifiers), and I also have npcs that move around said grids using lerp (although I do plan to find a different way to do so in the future), and it works well enough
however, the problem for me is that I want to know which grid the npc is currently above pretty much at all times. I haven’t tried spatial queries, because I’ve heard that raycasts are more efficient performance wise, and I haven’t used region3 because it’s deprecated
-I have used short (1-2 stud) downcasts, however, with them, updating 200 npcs hiked script activity to ~20% as well as with frequent frame drops
-so I have moved on to storing the positions of all of the grids on the map (~1300) in a table and comparing the position of the npc to that, however, after playing around with it for a few days I couldn’t get the script activity lower than 11% for every 200 npcs it has to update, although I haven’t noticed nearly as many frame drops with it
here is the current setup I am using
local tabletoinclude = {}
for i, v in ipairs(workspace.CloseUpGrids:GetDescendants()) do
if v.Name == "Grid" then
table.insert(tabletoinclude, v)
end
end
local postable = {}
for i, v in ipairs(tabletoinclude) do
postable[v.Position] = v
end
while wait(.1) do
for i, folder in ipairs(workspace.People:GetChildren()) do
for j, workermodel in ipairs(folder:GetChildren()) do
if workermodel:IsA('ObjectValue') then continue end
local humanoidpos = workermodel.HumanoidRootPart.Position
local closest = 99999 --didn't use math.huge because i am unsure of how performance heavy getting/operating with the constant assigned to it is
local chosenpos
for position, v in pairs(postable) do
local distance = (humanoidpos - position).Magnitude
if distance < closest then
closest = distance
chosenpos = position
end
end
game.ReplicatedStorage.Workers:FindFirstChild(workermodel.ID.Value, true).CurrentGrid.Value = postable[chosenpos]
end
end
end
any advice?