Tower shoots nothing when its target is nil

I have a TD script for a tower. A certain tower shoots 5 times and reloads. problem is, after it reloads, it shoots nothing (assuming its target is nil it shouldnt run). any help?

function tower.Attack(newTower, player)
	local config = newTower.Config
	local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.Value)
	
	if target ~= nil and target:FindFirstChild("Humanoid") then -- here is where it checks if the target isnt nil, yet, it still shoots.
		if newTower.Name == "Plasmagunner" or newTower.Name == "Plasmagunner2" then
			if config.HitsBeforeReload.Value > 0 then
				config:FindFirstChild("Reloading").Value = false
				config.HitsBeforeReload.Value -= 1
			else
				if config:FindFirstChild("Reloading").Value == false then
					config:FindFirstChild("Reloading").Value = true
					AnimateEvent:FireAllClients(newTower, "Reload")
					wait(0.2)
					newTower.HumanoidRootPart.Reload:Play()
					wait(2.8)
					config:FindFirstChild("Reloading").Value = false
					config.HitsBeforeReload.Value = 5
					target = nil
				end
			end
		else
			--abilities
			if newTower then
				if config:FindFirstChild("OriginalTower").Value then
					if config:FindFirstChild("OriginalTower").Value == "Mercenary" then
						local humanoid = target:FindFirstChildOfClass("Humanoid")

						if humanoid and humanoid:IsA("Humanoid") then
							humanoid.Died:Connect(function()
								player.leaderstats.Money.Value += newTower.Config.MoneyPerKill.Value
								if newTower.Head then
									newTower.Head.MoneyGain.TextLabel.Visible = true
									newTower.Head.MoneyGain.TextLabel.Text = "$"..newTower.Config.MoneyPerKill.Value
									newTower.Head.MoneyGain.TextLabel.TextTransparency = 0
									newTower.Head.Cash:Play()
									for i = 1,10 do
										if newTower.Head then
											newTower.Head.MoneyGain.TextLabel.TextTransparency += 0.1
											newTower.Head.MoneyGain.ExtentsOffset += Vector3.new(0, 0.1, 0)
											wait(0.1)
										end
									end
									newTower:FindFirstChild("Head"):FindFirstChild("MoneyGain").TextLabel.Visible = false
								end
							end)
						end
					end
				end
			end
		end
		if target then
			local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		
			newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		end

		AnimateEvent:FireAllClients(newTower, "Attack")


		for i, plrs in ipairs(game.Players:GetPlayers()) do 
			if target then
				if target:FindFirstChild("Humanoid").Health < config.Damage.Value then
					plrs.leaderstats.Money.Value += target:FindFirstChild("Humanoid").Health
				else
					plrs.leaderstats.Money.Value += config.Damage.Value
				end
			end
		end
		
		if target then
			if target:FindFirstChild("Humanoid").Health < config.Damage.Value then
				target:FindFirstChild("Humanoid"):TakeDamage(target.Humanoid.Health)
			else
				target:FindFirstChild("Humanoid"):TakeDamage(config.Damage.Value)
			end
		end

		local sound = Instance.new("Sound")
		sound.SoundId = newTower.HumanoidRootPart.Attack.SoundId
		sound.Parent = newTower.HumanoidRootPart
		sound.RollOffMode = Enum.RollOffMode.Linear
		sound.PlayOnRemove = true

		local random = math.random(1,2)
		if random == 1 then
			sound.PlaybackSpeed = 1
			sound:Destroy()
		elseif random == 2 then
			sound.PlaybackSpeed = 0.8
			sound:Destroy()
		end
		task.wait(config.Cooldown.Value)
	end
	
    task.wait(0.05)
	
	if newTower and newTower.Parent then
		tower.Attack(newTower, player)
	end
end
1 Like

do you set the target again after reloading?

hmm… I did realise it doesnt check if the target is nil AFTER reloading, only before.

thanks for reminding me i needed to set the target, not set it to nil so it shoots nothing. It works now.