How do I implement a "Reload after _ hits" into my TD script?

I have a tower script, and it works. I want to add a “reload after _ hits” for certain towers. how would I do this?

function tower.Attack(newTower, player)
	local config = newTower.Config
	local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.Value)
	
	if target and target:FindFirstChild("Humanoid") then
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		
		AnimateEvent:FireAllClients(newTower, "Attack")
		
		if target.Humanoid.Jump ~= true then
			for i, plrs in ipairs(game.Players:GetPlayers()) do  
				if target.Humanoid.Health < config.Damage.Value then
					plrs.leaderstats.Money.Value += target.Humanoid.Health
				else
					plrs.leaderstats.Money.Value += config.Damage.Value
				end
			end
		end

		if target.Humanoid.Jump ~= true then
			if target.Humanoid.Health < config.Damage.Value then
				target.Humanoid:TakeDamage(target.Humanoid.Health)
			else
				target.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)
		
		--abilities
		if newTower 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
    task.wait(0.05)
	
	if newTower and newTower.Parent then
		tower.Attack(newTower, player)
	end
end
1 Like

you can make a value for how many hits and then after every time it shoots subtract it by 1. if it it hits zero you can just reload and reset the hits again

do i add the value inside of the function? because this is one script controlling all towers.

idk maybe add an IntValue in the tower so it can be accessed as tower.Hits

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