Do you know any clever algorithm to optimize a constantly running large stream of actions?

I have a problem with machine gunners, when there are a lot of them, they load the map because of their firing speed at the maximum level (while wait(0.066)) and all I’ve come up with now is to save the damage done to some array or the like, and then, as the server’s TPS increases, implement instead heaps of repetitions

ive installed the bridgenet2 also (Features | BridgeNet2) to sync clients with server that have low TPS

The script of the tower that shoots so fast is just begins by declaring several variables that reference various objects and properties in the game, including the tower itself, a group of enemies, and the player who owns the tower.

The script then defines a function called CheckBonuses, which checks for any bonuses that the tower may have and adjusts the tower’s damage, range, and fire rate accordingly. The function also includes some error handling in case the tower does not have any bonuses.
Next, the script defines a function called chooseTarget, which iterates over the list of enemies and selects the one with the highest visibility value that is within range of the tower. The function takes into account whether or not the tower can see hidden enemies.
The script then defines a function called createSound, which creates a new sound object and sets its properties based on input parameters. This function is used later in the script to create a sound effect when the tower fires.
The script then enters an infinite loop, which repeatedly calls CheckBonuses and chooseTarget. If a target is found, the script calculates the direction in which to shoot and rotates the tower’s gun to face the target. The script then plays a sound effect and reduces the target’s health by the tower’s damage value. If the target is killed, the script rewards the player with money based on the amount of health the target had remaining.
Finally, the script uses the wait function to delay for the amount of time specified by the tower’s fire rate, then repeats the loop.

local tower = script.Parent
local mobs = workspace.Map.Mobs
local owner = tower.Parent:WaitForChild("Owner")

local origfieldOfViewRadius = script.Parent.Radius.Value
local origdamage = script.Parent.Damage.Value
local origfirerate = script.Parent.FireRate.Value

local finalfieldOfViewRadius = origfieldOfViewRadius
local finaldamage = origdamage
local finalfirerate = origfirerate

local function CheckBonuses()
	local DIP = tower:FindFirstChild("DamageIncreasePercent")
	
	if DIP then
		DIP = DIP.Value
		local FRIP = tower:FindFirstChild("FireRateIncreasePercent").Value
		local RIP = tower:FindFirstChild("RadiusIncreasePercent").Value
		
		
		

		finaldamage = origdamage+(origdamage*(DIP/100))
		finalfieldOfViewRadius = origfieldOfViewRadius+(origfieldOfViewRadius*(RIP/100))
		finalfirerate = origfirerate-(origfirerate*(FRIP/100))
	else
		finalfieldOfViewRadius = origfieldOfViewRadius
		finaldamage = origdamage
		finalfirerate = origfirerate
	end
end

-- Choose the enemy with the highest visibility value as the target
function chooseTarget()
	local highestVisibility = 0
	local target = nil

	for i, enemy in ipairs(mobs:GetChildren()) do
		if tower.CanHidden.Value == true then
			local position1 = Vector3.new(tower.Position.X, 0, tower.Position.Z)
			local position2 = Vector3.new(enemy.HumanoidRootPart.Position.X, 0, enemy.HumanoidRootPart.Position.Z)

			local distance = (position1 - position2).Magnitude

			if distance<=finalfieldOfViewRadius and enemy.Visibility.Value > highestVisibility then
				highestVisibility = enemy.Visibility.Value
				target = enemy
			end
		else
			if enemy.Hidden.Value == false then -- check if the mob is not hidden
				local position1 = Vector3.new(tower.Position.X, 0, tower.Position.Z)
				local position2 = Vector3.new(enemy.HumanoidRootPart.Position.X, 0, enemy.HumanoidRootPart.Position.Z)

				local distance = (position1 - position2).Magnitude

				if distance <= finalfieldOfViewRadius and enemy.Visibility.Value > highestVisibility then
					highestVisibility = enemy.Visibility.Value
					target = enemy
				end
			end
		end
	end
	return target
end

local function createSound(soundName, parent, soundid, speed)
	local sound = Instance.new("Sound")
	sound.Name = soundName
	sound.Parent = parent
	sound.PlaybackSpeed = speed
	sound.SoundId = "http://www.roblox.com/asset?id="..tostring(soundid)
	return sound
end

local shootSound = createSound("shoot", script.Parent.Gun.PrimaryPart, 1336753453, 0.75)

while true do
	CheckBonuses(); local target = chooseTarget()
	if target then
		if target.Humanoid.Health - script.Parent.Damage.Value <= 0 then
			local primaryPart = script.Parent.Gun.PrimaryPart
			local direction = (target.HumanoidRootPart.Position - primaryPart.Position) * Vector3.new(1,0,1)

			-- Create a new CFrame that looks at the target and adds a 180 degree rotation around the y-axis
			local newCFrame = CFrame.lookAt(primaryPart.Position, primaryPart.Position + direction) * CFrame.fromOrientation(0, math.rad(180), 0)

			-- Apply the new CFrame to the model
			script.Parent.Gun:SetPrimaryPartCFrame(newCFrame)
			
			game:GetService("Players")[tostring(owner.Value)].money.Value += math.abs(target.Humanoid.Health)
			
			shootSound:Play()
			target.Humanoid:TakeDamage(finaldamage)
		else
			local primaryPart = script.Parent.Gun.PrimaryPart
			local direction = (target.HumanoidRootPart.Position - primaryPart.Position) * Vector3.new(1,0,1)

			-- Create a new CFrame that looks at the target and adds a 180 degree rotation around the y-axis
			local newCFrame = CFrame.lookAt(primaryPart.Position, primaryPart.Position + direction) * CFrame.fromOrientation(0, math.rad(180), 0)

			-- Apply the new CFrame to the model
			script.Parent.Gun:SetPrimaryPartCFrame(newCFrame)
			
			game:GetService("Players")[tostring(owner.Value)].money.Value += math.abs(script.Parent.Damage.Value)
			
			shootSound:Play()
			target.Humanoid:TakeDamage(finaldamage)
		end
	end
	task.wait(finalfirerate)
end

i’ve already tried to increase the fire delay and increade damage proportionally, but it looks very poor and it is used more as a crutch. I need the exactsolution ar a good ideas to code that solution. I’m thinking about finding the perfect fastest solution, we will try for a long time, but I’m ready to pay robux to someone who really proves himself well. please feel free to write even nonsense like mine

im trying to use the :PivotTo() instead of ::SetPrimaryPartCFrame()