sorry for the non descriptive title, cant think of anything else, but i’ve been working on a tower defense game and theres a mechanic i cannot implement
so i have a module script for the towers and here is the function that handles the attacking:
function tower.Attack(newTower,player,name)
local config = newTower:FindFirstChild("Config")
local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.Value)
if target and target.Humanoid and target.Humanoid.Health > 0 then
local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
events:WaitForChild("AnimateTower"):FireAllClients(newTower, "Attack", target)
local module = require(script:FindFirstChild(newTower.Name,true))
module.Attack(newTower,target)
task.wait(config.Cooldown.Value)
end
task.wait(0.1)
if newTower and newTower.Parent then
tower.Attack(newTower,player)
end
end
so the main part is this here:
local module = require(script:FindFirstChild(newTower.Name,true))
module.Attack(newTower,target)
basically what this is, is that every tower has a seperate module script that does their attacks, and this just finds that module script and calls the attack function.
So now that that’s clear, here is the seperate module script that does the stuff for this specific tower:
local Module = {}
function Module.Attack(tower,target)
local config = tower.Config
if target and target.Humanoid and target.Humanoid.Health > 0 then
repeat
task.wait()
target.Humanoid:TakeDamage(15)
until target == nil
end
end
return Module
so basically after the if target, there would just be takedamage and that would be it but what i want for this tower, is basically that it keeps dealing continuous damage until the enemy is dead
anyways here is the issue
the target gets shot even if it is out range
so it repeats it until the target is nil, issue is that the target is never actually nil. In the attack function in the main script the attack function gets called only if it has already found a target, which is obviously an issue because it cannot detect if there is no target, therefore damaging the enemy even when it is out of range
the main script would be checking if it is in range, but since we are doing a repeat that means its not getting back to the main script, thats is also why i am trying to do the check in here
it seems like a simple issue but i have tried been trying for 2 days now and there is always something wrong with the solution i come up with, so if anyone has any ideas let me know