Wait and task.wait causing crash?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i would like the code to run without crashing

  2. What is the issue? Include screenshots / videos if possible!
    it crashes

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i have tried replacing task.wait with wait

for some odd reason it only occurs with one my my towers in my tower defense game

here is my code:

local Tower = {}

local RunService = game:GetService("RunService")
local TowerModule = require(game.ReplicatedStorage:WaitForChild("Tower Module"))
local EnemyPlacementModule = require(game.ServerScriptService:WaitForChild("Enemy Placement Module"))

function Tower.Run(Tower)
	local UpgradedEvent = Tower:WaitForChild("Upgraded")

	local Targeting = false
	local Target = nil
	local Connection
	local ProjectileOrigins = {}

	local Data = TowerModule.GetData(Tower)
	
	for i, instance in ipairs(Tower:GetChildren()) do
		if instance.name == "Fire Part" then
			table.insert(ProjectileOrigins, {instance, Tower.Humanoid:LoadAnimation(instance.Fire)})
		end
	end
	local NextOrigin = ProjectileOrigins[1]

	local function FindTarget()
		local Farthest = {}
		local First = true
		for Enemy, Time in pairs(EnemyPlacementModule) do
			if Enemy.Parent and (Tower.PrimaryPart.Position - Enemy.PrimaryPart.Position).Magnitude <= Data.Range then
				if First then
					First = false
					Farthest = {Enemy, Time}
				elseif Time > Farthest[2] then
					Farthest = {Enemy, Time}
				end	
			end
		end
		Target = Farthest[1]
	end

	local function Attack()
		local Index = 0
		while Target do
			Index += 1
			print("Loop", Index)
			Targeting = true
			local BasePosition = Target.PrimaryPart.Position
			Tower:PivotTo(CFrame.lookAt(Tower.PrimaryPart.Position, Vector3.new(BasePosition.X, Tower.PrimaryPart.Position.Y, BasePosition.Z)))
			local Bullet = Instance.new("Part")
			Bullet.Name = "Bullet"
			Bullet.Size = Vector3.new(0.1, 0.1, (NextOrigin[1].Position - Target.PrimaryPart.Position).Magnitude)
			Bullet.Anchored = true
			Bullet.CanCollide = false
			Bullet.CanTouch = false
			Bullet.CanQuery = false
			Bullet.Color = Color3.new(1, 1, 0)
			Bullet.CFrame = CFrame.lookAt(NextOrigin[1].Position, Target.PrimaryPart.Position)
			Bullet.CFrame = Bullet.CFrame:ToWorldSpace(CFrame.new(Vector3.new(0, 0, -Bullet.Size.Z / 2)))
			Bullet.Parent = workspace
			game.Debris:AddItem(Bullet, 0.05)
			NextOrigin[2]:Play()
	
			if #ProjectileOrigins > 1 then
				NextOrigin = ProjectileOrigins[table.find(ProjectileOrigins, NextOrigin) + 1] or ProjectileOrigins[1]
			end 
			
			Target.Humanoid:TakeDamage(Data.Damage)
			print("After humanoid Damage") -- Breakpoint here
			task.wait(1) -- Crashes here?
			print("Pre function call") -- Breakpoint here (never happens because of crash)
			FindTarget()
			print("Atfer function call")
		end
		Targeting = false
		Connection = RunService.Heartbeat:Connect(function()
			FindTarget()
			if Target then
				Connection:Disconnect()
				Attack()
			end
		end)
	end

	UpgradedEvent.Event:Connect(function()
		Data = TowerModule.GetData(Tower)
	end)

	Connection = RunService.Heartbeat:Connect(function()
		FindTarget()
		if Target then
			Connection:Disconnect()
			Attack()
		end
	end)
	
	Tower.ClickDetector.MouseClick:Connect(function(Player)
		game.ReplicatedStorage["Activate GUI"]:FireClient(Player, Tower)
	end)
end

return Tower
  1. You have heartbeat loops that run without any debounce statements
  2. Your while loop in attack has no sort of wait function due to this the script crashes because of how fast its going

I may be missing something but

why the heck would heartbeat require debounce

There is a clear task.wait(1) in my while true loop

Ok so apparently the reason it was crashing was the fire animation was empty (it had no poses)
idk why that made the game crash but glad it’s working now

1 Like