Gold earned keeps multiplying

Ive tried swicthing the if statement around and changing a few things but i still get the same issue where when i place my tower down itll multiply the gold earned by the amount of that tower placed. For example i place 4 farms down i earned 400 per attack but if i have 1 placed i earn 100 per attack. also having a issue where the support towers still try to aim towards the direction itd be attacking
Heres my attack function

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") and target.Humanoid.Health > 0 then
	local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
	newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame

	local damage = config.BaseDamage.Value --the base damage of the tower
	print(damage)

	for i, buffval in pairs(config.Damage:GetChildren()) do--affecting buffs
		local buff = buffval.Value --will lead to the tower's buff

		if buff.Parent then
			local tower = buff.Parent.Parent.Parent

			if not tower or not tower.Parent then
				buffval:Destroy()
			else
				local newpos = newTower.HumanoidRootPart.CFrame.Position
				local pos = tower.HumanoidRootPart.CFrame.Position
				local range = buff.Parent.Parent.Range.Value

				if (pos-newpos).Magnitude > range then --towers can move maybe?
					buffval:Destroy()
				end
			end
		else
			buffval:Destroy()
		end
	end

	for i,tower in pairs(workspace.Towers:GetChildren()) do-- just gonna call it towerfolder
		--have some check to determine if it's a support such as this
		if tower.Config.Type.Value == "Farm" then
			local PointsValue = 100	
			player.Gold.Value += PointsValue
	
		elseif tower.Config.Type.Value == "Support" then --having a type config can be very useful
			local bufftype = tower.Config.BuffType.Value --the type of buff?(only works if has one buff)
			local buff = tower.Config.Buff.Value --how much it buffs (only works if has one buff)
			local range = tower.Config.Range.Value -- the range of the buff

			if bufftype == "Damage" and (newTower.HumanoidRootPart.CFrame.Position-tower.HumanoidRootPart.CFrame.Position).Magnitude <= range then
				print("Applying buff")
				damage *= 1+buff --assuming the buff is 0.10 if +10%, etc

				local newbuff = Instance.new("ObjectValue")
				newbuff.Name = "Buff"
				newbuff.Value = tower.Config.Buff
				newbuff.Parent = tower.Config.Damage
			end
		end

	end

	print(damage)
	animateTowerEvent:FireAllClients(newTower, "Attack")
	target.Humanoid:TakeDamage(damage)
	config.Damage.Value = damage

	if target.Humanoid.Health <= 0 then
		player.Gold.Value += target.Humanoid.MaxHealth
		player.Kills.Value += 1
	end

	task.wait(config.Cooldown.Value)
end

task.wait(0.1)

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

What im trying to achieve is where when i place the farm itll award the player gold every time the cooldown finishes. And would like it where my support towers or farm tower dont run the attack animation and dont try to aim towards the enemy. Dont expect any code to written out. Im pretty sure i missused something somewhere

Please try and re-explain your goal, what issue you’re facing right now, and what you’ve tried so far.

My goal is to have it where the farm tower when an enemy passes it itll award the player with gold. My issue is everything works like it should. I just have the major issue where if i have 2 farms placed. When the tower attacks it awards 200 when in all honestly it should just award 100 and not 200. Its award 100 at first but if place the second tower it starts awarding 200 from each tower if they attack. Ive mainly tried move the if statement(Where it awards the gold if it a farm type) and it works fine no error but like i said it just keep multipying each time i place a farm tower. Hope that makes sense

Is the function called for each time a tower attacks, or does it run on some kind of beat/update loop?

Yes its called whenever the tower attacks

You loop through every present tower here. Which will cause the money to go up for each money tower there is

	for i,tower in pairs(workspace.Towers:GetChildren()) do-- just gonna call it towerfolder
		--have some check to determine if it's a support such as this
		if tower.Config.Type.Value == "Farm" then
			local PointsValue = 100	
			player.Gold.Value += PointsValue

So would i loop through the towers config folders instead of the tower themselves?

You shouldn’t have a loop, if you’re only checking for one tower, right?

Yeah im only checking if the towers a farm tower

Yes, every tower in the game. You said that function is called when every individual tower is attacking, that means if you have two tower, two towers will run that function. But let’s say both are farms, they both loop through each other, and checks true on each-other being a farm (which gives 400 gold instead of the intended 200)

Is newTower, the tower that attacked?

function tower.Attack(newTower, player)

if so, just do this without any loop.

if newTower.Config.Type.Value == "Farm" then

Yes newTower is the tower thats attacking and i do it without the loop and it breaks the support tower part.

Just don’t have the farms as a part of the for loop then.

Literally Thank you lol. Took it out the for loop and put it somewhere else and the points are awarded properly and the tower still get buffed

Also make sure, that it’s only the support/areaOfEffect-towers, that have that loop.

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