Player Pos Table

Im trying to create a table so that the distance from my SEnt to its target is logged in a table so that it can be recorded very frequently and if my SEnt loses sight with the target it can go back to a last known distance when it had a sight and go to there instead?

Any help wouldd be great
Thanks

[lua]function ENT:Initialize()
self.NodeLimit = 1024;
self.Nodes = {};
end

function ENT:GetTargetOrLOS( )
if #self.Nodes > self.NodeLimit then
local remove = #self.Nodes - self.NodeLimit;
for i=1, remove do
table.remove(self.Nodes, i);
end
end
self.Nodes[#self.Nodes+1] = self.Target:GetPos();
if self:HasLOS() then
return self.Target:GetPos()
else
for i=#self.Nodes, 0, -1 do
if self:HasLOS(self.Nodes*) then
return self.Nodes*;
end
end
end
end

function ENT:HasLOS(pos)
local pos = pos or self.Target:GetPos();
local ang = pos - self:GetPos();
local tr = util.QuickTrace(self:GetPos(), ang, {self, self.Target});
return !tr.Hit;
end[/lua]

This is how I did it in my Killer Melons. self.Target is it’s target.

[editline]10:13PM[/editline]

You want to call that function in the Think hook and it will return the position of the target if it can see it, else it will return the last position it had LOS that it can currently see. It will also add the current position to the Node table.