I have a script that will play an animation when the mouse button is clicked and it will do damage. Though I also wanted to make a looping animation for the script. Basically the animation loops so it makes the player look like there holding the spear instead of the default way of holding a tool. So I made a while loop so that the person would hold the spear differently. Though it isn’t working.
The area of the script that has the animations:
local InputService = game:GetService("UserInputService")
local InputType = Enum.UserInputType
local animation = nil
local slashAnimation = nil
tool.Equipped:Connect(function()
animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7162716543"
slashAnimation = humanoid:LoadAnimation(animation)
while true do
animation2 = Instance.new("Animation")
animation2.AnimationId = "rxassetid://7162740569"
IdleAnimation = humanoid:LoadAnimation(animation2)
IdleAnimation:Play()
end
end)
tool.Unequipped:Connect(function()
animation:Destroy()
slashAnimation = nil
animation2:Destroy()
IdleAnimation= nil
end)
True. It’s also possible that loading them tons of times in a while loop could affect the way they play. Try loading the animations outside of the function where you define variables (probably under slashAnimation). That way they A) have time to load and B) aren’t continuously being loaded during the loop. In regard to the actual animation looping itself, try using AnimationTrack.Completed:wait() so that way the animation isn’t constantly being reset.
In the end, it should look kind of like this (if you wish you can still change humanoid:LoadAnimation() to use Animators, I just don’t know how).
local InputService = game:GetService("UserInputService")
local InputType = Enum.UserInputType
local tool = script.Parent
local humanoid = tool.Handklanmfkamfagawgw3ergt34
local animation = nil
local slashAnimation = nil
animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7162716543"
slashAnimation = humanoid:LoadAnimation(animation)
animation2 = Instance.new("Animation")
animation2.AnimationId = "rxassetid://7162740569"
IdleAnimation = humanoid:LoadAnimation(animation2)
tool.Equipped:Connect(function()
while true do
IdleAnimation:Play()
IdleAnimation.Completed:wait()
end
end)
tool.Unequipped:Connect(function()
animation:Destroy()
slashAnimation = nil
animation2:Destroy()
IdleAnimation = nil
end)
local animation = Instance.new("Animation")
local animation2 = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7162716543"
animation2.AnimationId = "rxassetid://7162740569"
animation = humanoid.Animator:LoadAnimation(animation)
animation2 = humanoid.Animator:LoadAnimation(animation2)
local equip = false
animation.Priority = Enum.AnimationPriority.Action
animation.Looped = true
animation2.Priority = Enum.AnimationPriority.Action
animation.Looped = false
tool.Equipped:Connect(function()
equip = true
end)
tool.Unequipped:Connect(function()
equip = false
end)
while wait() do
if equip then
animation:Play()
wait(1)
animation2:Play()
while equip do
wait()
end
else
animation:Stop()
animation2:Stop()
while not equip do
wait()
end
end
end
It doesn’t work. Maybe I’m just putting the script in wrong. This is my full script: (original)
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function OnTouch(partOther)
if db1 then
if dbAnim then
local character = partOther.Parent
local humanOther = character:FindFirstChild("Humanoid")
if not humanOther then return end
if humanOther.Parent == tool then return end
local player = Players:GetPlayerFromCharacter(character)
if player and (player == Player or IsTeamMate(Player, player)) then
return
end
UntagHumanoid(humanOther)
TagHumanoid(humanOther, Player)
humanOther:TakeDamage(damage)
db1 = false
end
end
end
local InputService = game:GetService("UserInputService")
local InputType = Enum.UserInputType
local animation = nil
local slashAnimation = nil
tool.Equipped:Connect(function()
animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7162716543"
slashAnimation = humanoid:LoadAnimation(animation)
while true do
animation2 = Instance.new("Animation")
character.Animate.idle.Animation.AnimationId = "rxassetid://7162740569"
IdleAnimation = humanoid.Animator:LoadAnimation(animation2)
IdleAnimation.Looped=true
IdleAnimation:Play()
end
end)
tool.Unequipped:Connect(function()
animation:Destroy()
slashAnimation = nil
animation2:Destroy()
IdleAnimation= nil
end)
local debounce = false
InputService.InputBegan:Connect(function(input, processed)
if input.UserInputType == InputType.MouseButton1 and slashAnimation and not processed then
if debounce == false then
debounce = true
slashAnimation:Play()
local Connection
local tool = script.Parent
local function onTouch(partOther)
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
if not humanOther then return end
if humanOther.Parent == tool then return end
humanOther:TakeDamage(65)
if humanOther.Health <= 0 then
player.leaderstats.Souls.Value = player.leaderstats.Souls.Value + 1
humanOther.Parent:Destroy()
end
end
Connection = tool.Handle.Touched:Connect(onTouch)
slashAnimation.Stopped:Wait()
debounce = false
Connection:Disconnect()
wait(.2)
debounce = false
end
end
end)