Help with TD damage script

So, my module script has a damage function to damage the target. problem is, when i test with 2 people, the damage is twice as much as before (1 is the original damage), same if i test with 3, 3 damage instead of 1. (almost as if it was a local script firing to the server, which it isnt.)

function tower.Attack(newTower, player)
   local config = newTower.Config
   local target =	findNearestTarget(newTower, config.Range.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
   				target.Humanoid:TakeDamage(target.Humanoid.Health)
   			else
   				plrs.leaderstats.Money.Value += config.Damage.Value
   				target.Humanoid:TakeDamage(config.Damage.Value)
   			end
   		end
   	end
   	
   	local sound = Instance.new("Sound")
   	sound.SoundId = newTower.HumanoidRootPart.Attack.SoundId
   	sound.Parent = newTower.HumanoidRootPart
   	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)
   	
   	if newTower.Name == "Mercenary" then
   		target:FindFirstChild("Humanoid").Died:Connect(function()
   			player.leaderstats.Money.Value += 1000
   		end)
   	end
   end
   task.wait(0.05)
   
   if newTower and newTower.Parent then
   	tower.Attack(newTower, player)
   end
end

Only skimmed through the code, but I assume here is the issue:

   	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
   				target.Humanoid:TakeDamage(target.Humanoid.Health)
   			else
   				plrs.leaderstats.Money.Value += config.Damage.Value
   				target.Humanoid:TakeDamage(config.Damage.Value)
   			end
   		end
   	end

Probably need to add a little break to that for loop:

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

It’s damaging for every player to the same target which was defined with target = findNearestTarget(newTower, config.Range.Value)

This seem right?

but wouldnt that stop it from looping through every player to give them the money?

Ah right, then just pop it out of the for loop.

    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
1 Like

it works! thx.

limit

1 Like

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