How do i remove this without any errors

  1. What do you want to achieve? I wanna remove the task.wait(0.01) at the end of the script because it causes delay on tower Firerate

  2. What is the issue? If i remove it i get a error saying something like Stack overflow

  3. What solutions have you tried so far? I dont know how to fix this.

function tower.Attack(newTower, player)
	local config = newTower:FindFirstChild("Config")
	local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.Value)
	
	if target and target:FindFirstChildOfClass("Humanoid") and not config:FindFirstChild("Burst") and target.Humanoid.Health > 0 then
		
		enemyHighlightEvent:FireAllClients(target)
		
		if target.Humanoid.Health < config.Damage.Value then
			player.Cash.Value += target.Humanoid.Health
		else
			player.Cash.Value += config.Damage.Value
		end
		
	 	if newTower.Animations.Fire:FindFirstChild("Attack") then
			animateTowerEvent:FireAllClients(newTower, "Fire", "Attack", target)
		elseif newTower.Animations.Fire:FindFirstChild("Attack1") and newTower.Animations.Fire:FindFirstChild("Attack2") and newTower.Animations.Fire:FindFirstChild("CurrentAnim") then
			animateMultipleTowerEvent:FireAllClients(newTower, "Fire", "Attack1", "Attack2", target)
		end

        tower.FaceTarget(newTower, target, 0.0)

		if newTower.Animations.Fire:FindFirstChild("Attack") then
			animateTowerEvent:FireAllClients(newTower, "Fire", "Attack", target)
		end	
		
		newTower.TotalDamage.Value += newTower.Config.Damage.Value
		
		target.Humanoid:TakeDamage(config.Damage.Value)
		
		if target.Humanoid.Health <= 0 then
			player.Kills.Value += 1
		end
		
		task.wait(config.Firerate.Value)
	end

	task.wait(0.01) -- Firerate delay

	if newTower and newTower.Parent then
		tower.Attack(newTower, player)
	end
end

this is a module script and im not going to show full script because theres alot of things

Try using RunService.Heartbeat:Wait()

This function should just be wrapped in a while-loop; you can’t do infinite recursions because everything needs to be stored in a stack, and you’re adding more and more to that stack every time you recurse


Additionally, make sure that your Remotes are UnreliableRemoteEvents instead of RemoteEvents; if you’re using regular remotes you’ll run into a RemoteExhaustion error

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.