You can write your topic however you want, but you need to answer these questions:
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
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”
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
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
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
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.
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?
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.
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.