Node radius script taking too much performance when its 4 cars (1 car = 1 node radius script)

As you know in my game, i have A*Star node algorithm, BUT it makes a new path every new node it completes, meaning its good for chase AI and etc, BUT when its used on LONG noded maps, there is a problem it causes a lag spike, since its generating a path every new node on a long one, so to fix this i made a radius script that uses a node folder inside of the car, and works well, BUT takes alot of performance. basically this node radius script is like ‘‘chunk-generating’’ to prevent lag spike from extremely long paths. Alright lets get to the point where i mean with ‘‘Node radius’’, basically this works like this. First of all there is a ‘‘Nodes’’ Folder in workspace, THEN this car’s ‘‘node radius’’ will get all the nodes in a estimated range, and remove nodes that are out of range (does not remove nodes existing in workspace folder) so with this node radius system, Car chases will be extremely synchorized. But there is problem, ITS A HIGH PERFORMANCE usage, at first of all i thought it was because the node’s we’re too long in the nodes folder in workspace, BUT I WAS WRONG. IT EVEN STRUGGLES ON 4 NODES. so yeah, i don’t know really what to say:

local nodesFolder = script.Parent.Nodes
local radius = 110
local updateInterval = 0.02
local clonedNodes = {}

local function removeOutOfRangeParts(centerPosition)
	for _, part in ipairs(nodesFolder:GetChildren()) do
		local partPosition = part.Position
		if (partPosition - centerPosition).Magnitude > radius then
			clonedNodes[part] = nil
			part:Destroy()
		end
	end
end

while true do
	local centerPosition = script.Parent.BaseModel.Position
	local minPosition = centerPosition - Vector3.new(radius, radius, radius)
	local maxPosition = centerPosition + Vector3.new(radius, radius, radius)

	for _, part in ipairs(workspace.Nodes:GetDescendants()) do
		if part:IsA("BasePart") and (part.Position - centerPosition).Magnitude <= radius and not clonedNodes[part] then
			local newPart = part:Clone()
			newPart.Parent = nodesFolder
			newPart.Transparency = 1
			clonedNodes[part] = newPart
		end
	end

	removeOutOfRangeParts(centerPosition)

	task.wait(updateInterval)
end

1 Like

Update:
yeahaboutthat
I don’t know how is that causing it that much.