Towers not attacking mobs

i have this 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 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:

you forgot to put a newTower variable inside the “FindNearestTarget” function

function FindNearestTarget(newTower)

i fixed that however now, i have an attack anim which wont load

image

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

Can you check the output, if there is an error?

nope no error sadly idk what to do

i may have misspelt smth or smth idk

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)

it still works without the fix tho i just want it to work b4 i move on. notice any errors or spelling mistakes in the code?

i fixed it, however is it possible to speed up animations

animationTrack:AdjustSpeed("insert a number") -- change the animationTrack variable. if have to

where would i refer this and what variable of the script actually refers to the animation, as in how would i actually do this

I’d say here probably

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

how would i change the speed, when actually calling the thing (for individual animations)

i don’t actually know how to that :confused: you might aswell ask in their discord server.

Can you elaborate more on what you mean by that, like different animation speeds per enemy?

i have it fixed now, however when the tower isnt shooting, the animation keeps playing

Can you elaborate on animation?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.