Animation loop how to stop it?
Hi i am making a tower defense game and the animation for the towers attack keeps looping. This here is the code: (not the entire code)
function Tower.Attack(newTower)
local target = FindNearestTarget(newTower)
if target then
animateTowerEvent:FireAllClients(newTower, "Attack")
target.Humanoid:TakeDamage(40)
end
task.wait(1)
Tower.Attack(newTower)
end
here is the animation script:
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")
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")
workspace.Towers.ChildAdded:Connect(function(object)
playAnimation(object, "Idle")
end)
animateTowerEvent.OnClientEvent:Connect(function(tower, animName)
playAnimation(tower, animName)
end)
i relly hope the local script helps
Solution 1:
The Looped
property for your AnimationTrack
was probably set to true
when you created it in the animation editor. You could prevent the animation from looping in one of two ways:
- Edit the
Looped
property in the animation editor and update the animation. - Set the
Looped
property tofalse
in yourSetAnimation
function:
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")
local animationTrack = animator:LoadAnimation(animationObject)
animationTrack.Looped = false
return animationTrack
end
end
end