local closestCharacter = nil
local shortestDistance = self.Range
local towerPosition = self.Model.PrimaryPart.Position
for _, character in pairs(workspace.Characters:GetChildren()) do
if character and character.PrimaryPart then
local distance = (character.PrimaryPart.Position - towerPosition).Magnitude
if distance <= self.Range and distance < shortestDistance then
shortestDistance = distance
closestCharacter = character
end
end
end
return closestCharacter
end
function class:TargetFunction()
task.spawn(function()
while self.Model and self.Model.Parent do
local target = self:FindTarget()
if target then
self.Target = target
print(target.Name)
self:LookAtTarget(self.Target.PrimaryPart.Position)
else
self.Target = nil
end
task.wait()
end
end)
end
function class:LookAtTarget(targetPosition)
local modelPrimary = self.Model.PrimaryPart
if not modelPrimary then return end
local towerPosition = modelPrimary.Position
local targetVector = Vector3.new(targetPosition.X, towerPosition.Y, targetPosition.Z)
local targetCFrame = CFrame.new(towerPosition, targetVector)
modelPrimary.CFrame = targetCFrame
end
function Action(self)
if self.Characters.Length == 0 then
self.ActionTask = nil
else
self.AttackAnimation:Play()
self.ActionTask = shared.Task:Delay(self.Cooldown, Action, self)
self:TargetFunction()
self:Action()
end
end
return class``` This is my code, it still didn't do anything. the tower still rotates even on cooldown