#Top Down Tower Defense game help,

6 messages · Page 1 of 1 (latest)

woeful shale
#

Here is the enemy script local enemy = script.Parent
local humanoid = enemy:WaitForChild("Humanoid")
local attackRange = 1000
local attackRate = 1
local damage = 10
local target = workspace:WaitForChild("TowerModel")

local function moveToTarget()
humanoid:MoveTo(target.Position)
end

local function isInAttackRange()
local distance = (enemy.HumanoidRootPart.Position - target.Position).Magnitude
return distance <= attackRange
end

local function attackTarget()
if target:FindFirstChild("Humanoid") then
target.Humanoid:TakeDamage(damage)
end
end

while true do
if target then
if isInAttackRange() then
attackTarget()
else
moveToTarget()
end
end
wait(attackRate)
end

#

And here is the tower script

#

local tower = script.Parent
local health = 100
local maxHealth = 100

local function takeDamage(damage)
health = health - damage
if health <= 0 then
tower:Destroy()
end
end

local function repairTower(amount)
health = math.min(health + amount, maxHealth)
end

tower.ChildAdded:Connect(function(child)
if child:IsA("Part") then
child.Touched:Connect(function(hit)
if hit:IsA("Part") then
local attacker = hit.Parent
if attacker:IsDescendantOf(workspace.Enemies) then
takeDamage(10)
end
end
end)
end
end)

brazen nymph
#

In the enemy script it tries to damage a humanoid. If no humanoid is inside the tower, nothing will happen.