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
view source
01 local newTower = towerExists:Clone()
02 newTower.HumanoidRootPart.CFrame = cframe
03 newTower.Parent = workspace.Towers
04 newTower.HumanoidRootPart:SetNetworkOwner(nil)
05
06 local bodyGyro = Instance.new("BodyGyro")
07 bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
08 bodyGyro.D = 0
09 bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
10 bodyGyro.Parent = newTower.HumanoidRootPart
11
12 for i, object in ipairs(newTower:GetDescendants()) do
13 if object:IsA("BasePart") then
14 PhysicsService:SetPartCollisionGroup(object, "Tower")
15 end
View all 21 lines...
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)

1 comment
Hello, most of programmers here does not have time and patience to read all this! Look for output console and post the block of code that is associated with that error, or where do you think the script fails. I hope we will be able to help after! Have a nice day.