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 tower = {}
function FindNearestTarget()
local maxDistance = 20
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 then
target.Humanoid:TakeDamage(4)
end
task.wait(0.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)
for i, object in ipairs(newTower:GetDescendants()) do
if object:IsA("BasePart") then
object.CollisionGroup = "Towers"
end
end
coroutine.wrap(tower.Attack)(newTower)
else
warn("Requested tower could not be found:", name)
end
end
spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower
however the towers just dont attack the mobs, idk why this is.
im using a tutorial by gnomecode:
i fixed that however now, i have an attack anim which wont load
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)
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 = 20
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 then
target.Humanoid:TakeDamage(4)
end
task.wait(0.1)
animateTowerEvent:FireAllClients(newTower, "Attack")
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)
for i, object in ipairs(newTower:GetDescendants()) do
if object:IsA("BasePart") then
object.CollisionGroup = "Towers"
end
end
coroutine.wrap(tower.Attack)(newTower)
else
warn("Requested tower could not be found:", name)
end
end
spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower
I am not really sure where the error is, just watch the tutorial again and look at it throughly. ALSO, in the next episode he corrects a mistake, look at episode 8 (it’s in the beginning)
local function playAnimation(object, animName)
local animationTrack = setAnimation(object, animName)
if animationTrack then
animationTrack:Play()
animationTrack:AdjustSpeed("insert a number")
else
warn("Animation track does not exist")
return
end
end