Im trying to make a sword tower with combo in my tower defense game

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!
    im trying to make a sword tower with combo in my tower defense game

  2. What is the issue? Include screenshots / videos if possible!
    i tried putting humanoid:takedmg in a loop but it doesnt work well, i put it in a for loop like for i=1, DamageTime ( how many time i want it to dmg the enemy, i put a numbervalue in my tower config folder ) and tried putting if humanoid health == 0 or findfirstchild “humanoid” == nil then break the loop but cant get it working. after the mob died the loop still run and it look for the humanoid of that mob and cant find it and error “Humanoid is not a valid member of mob”

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried to put the mob in the table but cant get it working

function tower.Attack(newTower, player)
	local config = newTower.Config
	local target = FindNearestTarget(newTower, config.Range.Value)
	local mobFolder = workspace.Mobs
	task.wait(0.3)

	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 and not FindEntry(Damaged, target) then

		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)

		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame

		animateTowerEvent:FireAllClients(newTower, "Attack")

		task.wait(config.AnimationTimes.Value)	

		for i=1, config.DamageTimes.Value do
			task.wait(config.AnimationTimesPerAttack.Value)

			target.Humanoid:TakeDamage(config.Damage.Value)

			if target:FindFirstChild("Humanoid") == nil then
				break
			end
		end	
		
		if	target.Humanoid.Health <= 0 then
			target.Humanoid.Died:Connect(function()
				player.Gold.Value += config.Damage.Value / 3

			end)
		end
		task.wait(config.Cooldown.Value)
	end
	tower.Attack(newTower, player)
end

Should work, let me know if it doesn’t.

1 Like

still getting Humanoid is not a valid member of Model :frowning:

Apologies, I skimmed quickly through your code. It’s about 5 AM so excuse me lol.

        if not target:FindFirstChild("Humanoid") then
           return
        end

		Damaged = false

		if	target.Humanoid.Health <= 0 then
			target.Humanoid.Died:Connect(function()
				player.Gold.Value += config.Damage.Value / 3
				table.remove(Damaged, #Damaged)

			end)
		end
1 Like

still the humanoid is not a valid member of model :frowning:

If you could provide actual context as to where the error is happening, that’d be appreciated. (what line, column, etc)

its the target.Humanoid:TakeDamage(config.Damage.Value) inside the loop the tower cant find that humanoid after the mob died

Are you sure you replaced all the code that I modified? The loop should be breaking if the humanoid isn’t there.

		for i=1, config.DamageTimes.Value do
			if target:FindFirstChild("Humanoid") == nil then
				break
			end
			task.wait(config.AnimationTimesPerAttack.Value)

			target.Humanoid:TakeDamage(config.Damage.Value)
		end	
		
		if not target:FindFirstChild("Humanoid") then
			return
		end

		if	target.Humanoid.Health <= 0 then
			target.Humanoid.Died:Connect(function()
				player.Gold.Value += config.Damage.Value / 3


			end)
		end

Ah, I see why.

		for i=1, config.DamageTimes.Value do
		     task.wait(config.AnimationTimesPerAttack.Value)
             if target:FindFirstChild("Humanoid") == nil then
				break
			end

			target.Humanoid:TakeDamage(config.Damage.Value)
		end	

My bad. Basically, while the thread is sleeping (waiting), the humanoid is destroyed or removed.

task.wait(config.AnimationTimesPerAttack.Value)

is just basically how long does his swing the sword to the enemy here the video
my bad forgot to explain

tried this but now its doesnt return any error and after the mob died he doesnt switch to the next mob

That’s something up with your code man, I don’t know how your system functions or locates the next NPC. And it doesn’t return an error now which was the goal, no?

1 Like

here the video

Again, it seems to be something with your system on looking for the target. I recommend you mark this as solved as I solved your problem. Overlook your code again, do print debugging, etc.

1 Like

okay but i want you to look at my looking for the target system code cuz idk what is wrong

function FindNearestTarget(newTower, range)
	local nearestTarget = nil
	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < range then
			nearestTarget = target
			range = distance
		end
	end
	return nearestTarget
end

Once again, I recommend you overlook your code and print-debug things. There’s a high chance you made a small mistake somewhere. Since it’s nearly 6AM, I’m logging off for tonight. If your issue continues to persist, I can help you when I wake up or another scripter can assist you.

1 Like

okay thanks for trying to help have a good sleep buddy

fixed this by putting a humanoid.died function in it then rerun the tower.attack function inside the humanoid.died and turn out it work very well