The script calculating the position AI takes a cover makes game laggy when detects many coverable position

Recently, I’m making the AI which takes cover when it got hit. I’ve already made the part which calculate the covering position, but it gets too laggy if scripts detect many places to cover.

Script:

agentradius = 2
agentheight = 5

while wait(0.1) do
	for i = -20, 20, 4 do
		for v = -20, 20, 4 do
			local rayOrigin = game.Workspace.Dummy.Head.Position
			local rayDirection = (Vector3.new(i,5,v) - game.Workspace.Dummy.Head.Position).Unit * 100
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {game.Workspace.Dummy}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
			if raycastResult then
				local hitPart = raycastResult.Instance
				if hitPart == nil then
				else
					local Part = Instance.new("Part")
					game:GetService("Debris"):AddItem(Part,0.1)
					Part.Size = Vector3.new(0.25,0.25,0.25)
					Part.Shape = Enum.PartType.Ball
					Part.Anchored = true
					Part.CanCollide = false
					Part.Material = Enum.Material.Neon
					Part.Color = Color3.fromRGB(255, 255, 255)
					Part.Parent = workspace
					Part.Position = Vector3.new(i, agentheight, v)
					local secondrayOrigin = game.Workspace.Dummy.Head.Position
					local secondrayDirection = (Part.Position - game.Workspace.Dummy.Head.Position).Unit * 100
					local secondraycastParams = RaycastParams.new()
					secondraycastParams.FilterDescendantsInstances = {game.Workspace.Dummy}
					secondraycastParams.FilterType = Enum.RaycastFilterType.Blacklist
					local secondraycastResult = workspace:Raycast(secondrayOrigin, secondrayDirection, secondraycastParams)
					if secondraycastResult then
						local secondhitPart = secondraycastResult.Instance
						if secondhitPart == Part then
							Part:Destroy()
						else
							if (Part.Position - hitPart.Position).magnitude < hitPart.Size.X/3 + hitPart.Size.Y/3 + hitPart.Size.Z/3 + 5 then
								Part.Color = Color3.fromRGB(0, 255, 0)
								for twoI = -agentradius, agentradius, 2 do
									for twoV = -agentradius, agentradius, 2 do
										local SecondPart = Instance.new("Part")
										SecondPart.Size = Vector3.new(0.25,0.25,0.25)
										SecondPart.Shape = Enum.PartType.Ball
										SecondPart.Anchored = true
										SecondPart.CanCollide = false
										SecondPart.Material = Enum.Material.Neon
										SecondPart.Color = Color3.fromRGB(255, 255, 255)
										SecondPart.Parent = workspace
										SecondPart.Position = Vector3.new(i + twoI, agentheight, v + twoV)
										local thirdrayOrigin = game.Workspace.Dummy.Head.Position
										local thirdrayDirection = (SecondPart.Position - game.Workspace.Dummy.Head.Position).Unit * 100
										local thirdraycastParams = RaycastParams.new()
										thirdraycastParams.FilterDescendantsInstances = {game.Workspace.Dummy}
										thirdraycastParams.FilterType = Enum.RaycastFilterType.Blacklist
										local thirdraycastResult = workspace:Raycast(thirdrayOrigin, thirdrayDirection, thirdraycastParams)
										if thirdraycastResult then
											local thirdhitPart = thirdraycastResult.Instance
											if thirdhitPart == SecondPart then
												Part:Destroy()
												SecondPart:Destroy()
											else
												SecondPart:Destroy()
											end
										end
									end
								end
							else
								Part:Destroy()
							end
						end
					end
					Part.CollisionGroupId = 3
				end
			end
		end
	end
end

Video:

How should I fix that?

1 Like