Code is running poorly

So the below code, it lags like crazy over time. But as soon as I add the “print(“DELAY”)” command it stops lagging. I have no idea why? Like as soon as I comment that out, it’s back to lagging?

local function findTargets()
	--print("DELAY")
	-- Do a new search region if we are not already searching through an existing search region
	if not searchingForTargets and tick() - timeSearchEnded >= SEARCH_DELAY then -- search delay
		searchingForTargets = true

		-- Create a new region
		local centerPosition = Zombie.humanoidRootPart.Position
		local topCornerPosition = centerPosition + Vector3.new(ATTACK_RADIUS, ATTACK_RADIUS, ATTACK_RADIUS)
		local bottomCornerPosition = centerPosition + Vector3.new(-ATTACK_RADIUS, -ATTACK_RADIUS, -ATTACK_RADIUS)

		searchRegion = Region3.new(bottomCornerPosition, topCornerPosition)
		searchParts = Workspace:FindPartsInRegion3(searchRegion, Zombie.instance, math.huge)

		newTarget = nil
		newTargetDistance = nil

		-- Reset to defaults
		searchIndex = 1
	end

	if searchingForTargets then
		-- Search through our list of parts and find attackable humanoids
		local checkedParts = 0
		while searchingForTargets and searchIndex <= #searchParts and checkedParts < MAX_PARTS_PER_HEARTBEAT do
			local currentPart = searchParts[searchIndex]
			if currentPart and isInstaceAttackable(currentPart) then
				local character = currentPart.Parent
				local distance = (character.HumanoidRootPart.Position - Zombie.humanoidRootPart.Position).magnitude

				-- Determine if the charater is the closest
				if not newTargetDistance or distance < newTargetDistance then
					newTarget = character.HumanoidRootPart
					newTargetDistance = distance
					print(newTarget, "yes")
					else print ("no")
				end
			end

			searchIndex = searchIndex + 1

			checkedParts = checkedParts + 1
		end

		if searchIndex >= #searchParts then
			target = newTarget
			--print(target,"this is name")
			--print(newTargetDistance)
			searchingForTargets = false
			timeSearchEnded = tick()
		end
	end
end

while true do 
	wait()
	onHeartbeat()
end

https://gyazo.com/c79d16df29307a08e6bb8168493ba1b3 yea I don’t get this (this gyazo) is with a wait command before it runs. And still lags, yet if I add a print command no lag.

I cant really tell whats causing performance issues, but >= is not a valid operator in lua

It is a valid operator
image
Ran on the latest luau release, don’t mind it being a combined symbol, that’s just my font.

Oh bruh I always thought that >= was not a valid operator in lua :skull:

Region3 with math.huge is not needed as it will detect unneeded parts like the enviroment, tools, accessories potentially. Hence I suspect this is the main source of lag if ran a lot of times.

If you only need to find the player characters in a zone, consider more performant methods like zone +, or only looping through players:GetPlayers() for their humanoid root part and checking if they are in the zone and such.

also @Zenqpa it’s in the lua pil manual for >=

1 Like

You could also use the Tagging system of the CollectionService to cleanly define Humanoids on your world and loop through them, rather than using Regions.

I would include a developer hub link by, I’m on my phone and the hub isn’t working for me. :expressionless:

I changed math.huge to 20 and still same thing. I added a wait() instead of the print command and same result. I guess print is somehow delaying this code. Perhaps it’s just running too fast.

EDIT: Figured it out. EDIT: Figured it out. d

try using task.wait() instead of wait() at the end.