Improving a circuit continuity function

I have previously posted in the Scripting Support category about my inefficient script. If you want, you can read that for context. The reason I am posting here is because I found out that this category is for already existing code.

Script snippet:

local function GetTouchingPartsNew(part)
   local connection = part.Touched:Connect(function() end)
   local results = part:GetTouchingParts()
   connection:Disconnect()
   return results
end

function reset ()
    	currentcircuit = 1
    	for i,v in pairs (game.Workspace.CanvasObjects:GetChildren()) do
    		if v:FindFirstChild("Circuit") then
    			v.Circuit.Value = currentcircuit
    			currentcircuit = currentcircuit + 1
    		end
    	end
    end

    function update()
    	reset()
    	for i,v in pairs (game.Workspace.CanvasObjects:GetChildren()) do
    		
    		if v:FindFirstChild("zone") then
    		for i,d in pairs (GetTouchingPartsNew(v.zone)) do
    			local x = nil
    			if d.Name == "GridPart" then
    				x = d.Parent
    			end
    			local vibecheck = true
    			if v and x and v:FindFirstChild("Circuit") and x:FindFirstChild("Circuit") and v.Circuit.Value ~= x.Circuit.Value and v.Owner.Value == x.Owner.Value then
    				
    				if ItemModule[v.Name] and ItemModule[v.Name].whitelist then
    						if not ItemModule[v.Name].whitelist[x.Name] then
    							vibecheck = false
    						end
    					end
    				
    				if ItemModule[x.Name] and ItemModule[x.Name].whitelist then
    						if not ItemModule[x.Name].whitelist[v.Name] then
    							vibecheck = false
    						end
    				end
    				
    				if vibecheck == true then
    				if v.Circuit.Value > x.Circuit.Value then
    					v.Circuit.Value = x.Circuit.Value
    				else
    					x.Circuit.Value = v.Circuit.Value
    				end
    				end  
    			end
    		end
    		end
    	end
    end

Keep in mind that while there may be many errors in the above script, there is a large error that I can’t fix, and that is the delay. The delay seen by the server is absolutely ludicrous (look at the performance graph):

https://i.gyazo.com/e74ba801fc20bfe90cdc320d8b3daf9a.mp4

You may notice that there are blue hitboxes once construction is initiated. That is a placeholder for the Under Construction effect. The true delay is the frame lag (I am testing in Solo) once something is built, or destroyed (I call update in those two cases).

What do I want to fix? I want to fix this atrocious delay that is keeping my ping in the thousands. A true gamer like myself can’t handle that amount of delay.

What do I believe is causing the delay? I believe that the nested loops caused the delay. I mean think about it, you are looping through 100s of buildings and looping further through nearby buildings. I want another way.

How do I plan on fixing it? I want to call an update on some of the buildings but I don’t know which will be affected. I am wishing that a mathematical genius replies and gives me the perfect fix by the way (actually I just want a practical fix that’s all)

Thank you

trash.rbxl (16.8 KB) (may give errors because I deleted everything except for that snippet)

Update: I fixed the problem by using update() for only a specific area. I also found out that my ViewportFrame had also lagged my game, so I updated specific parts of that too. Instead of deleting all of the instances in the Viewport, I updated the client responsibly.

This was copied from my original post

1 Like