I can't figure this out. I did stop the repeating and made it action so it can't be that.

Video - https://streamable.com/ubq2fw Here's the script:

local PhysicsService = game:GetService("PhysicsService") local ServerStorage = game:GetService("ServerStorage") local ReplicatedStorage = game:GetService("ReplicatedStorage")

local events = ReplicatedStorage:WaitForChild("Events") local spawnTowerEvent = events:WaitForChild("SpawnTower") local animateTowerEvent = events:WaitForChild("AnimateTower")

local tower = {}

function FindNearestTarget(newTower) local maxDistance = 15 local nearestTarget = nil

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

return nearestTarget end

function tower.Attack(newTower) local target = FindNearestTarget(newTower)

if target and target:FindFirstChild("Humanoid") and target.Humanoid.health > 0 then

  local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
  newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
  
  animateTowerEvent:FireAllClients(newTower, "Attack")
  
  target.Humanoid:TakeDamage(5)

end

task.wait(1)

tower.Attack(newTower) end

function tower.Spawn(player, name,cframe) local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)

if towerExists then

      local newTower = towerExists:Clone()
  newTower.HumanoidRootPart.CFrame = cframe
  newTower.Parent = workspace.Towers
  newTower.HumanoidRootPart:SetNetworkOwner(nil)
  
  local bodyGyro = Instance.new("BodyGyro")
  bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
  bodyGyro.D = 0
  bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
  bodyGyro.Parent = newTower.HumanoidRootPart
      
      for i, object in ipairs(newTower:GetDescendants()) do
          if object:IsA("BasePart") then
              PhysicsService:SetPartCollisionGroup(object, "Tower")
          end
      end
  
      coroutine.wrap(tower.Attack)(newTower)
  else
      warn("Requested tower does not exist", name)
  end

end

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)

return tower

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local events = ReplicatedStorage:WaitForChild("Events") local animateTowerEvent = events:WaitForChild("AnimateTower")

local function setAnimation(object, animName) local humanoid = object:WaitForChild("Humanoid") local animationsFolder = object:WaitForChild("Animations")

if humanoid and animationsFolder then local animationObject = animationsFolder:WaitForChild(animName)

  if animationObject then
      local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
      local animationTrack = animator:LoadAnimation(animationObject)
      return animationTrack
  end

end end

local function playAnimation(object, animName) local animationTrack = setAnimation(object, animName)

if animationTrack then animationTrack:Play() else warn("Animation track does not exist.") return end end

workspace.Mobs.ChildAdded:Connect(function(object) playAnimation(object, "Walk") end)

workspace.Towers.ChildAdded:Connect(function(object) playAnimation(object, "Idle") end)

animateTowerEvent.OnClientEvent:Connect(function(tower, animName) playAnimation(tower, animName) end)

I legit can't figure it out. please help

2 comments

0

We can't really do anything if you won't give the entire script on the sending part and on the recieving part.

0

hold on