Spear animation loop

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)
2 Likes

Loading animation from humanoid is deprecated so you should use: Animator | Roblox Creator Documentation

And the animator is inside the humanoid.

2 Likes

Like this?

IdleAnimation = humanoid.animator:LoadAnimation(animation2)

Edit: i know I did something wrong since it doesnt work

1 Like

It should be like that:

IdleAnimation = humanoid.Animator:LoadAnimation(animation2)

Also, maybe you are not giving time for the animation to load to play it there.

I recommend doing this to change the idle animation of the player instead of a loop:

character.Animate.idle.Animation1.AnimationId = "rxassetid://7162740569"
2 Likes

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)
2 Likes

Try this:

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()
	else
		animation:Stop()
		animation2:Stop()
	end
end
2 Likes

Doesn’t work. Thanks for trying to help though.

1 Like

Sadly it also doesn’t work. Thanks for the help though.

2 Likes

Sadly it doesn’t work.


1 Like

How is it not working? Is the animation not playing at all or is the player frozen?

1 Like

none of the animations play at all. :confused:

1 Like

I forgot to put something. Try this:

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
2 Likes

Animation is owned by Roblox or you, right?

2 Likes

It’s possible that there’s a problem with the animations then.

1 Like

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)
2 Likes

owned by me.


1 Like

Maybe. I doubt it though.


1 Like

Another thing, is the priority properly set?

1 Like

Don’t know if this has been caught or not, but I believe it should be “rbxassetid”.

1 Like

Dun dun duuuun. A spelling error! Ok nice find I’ll change it tommarow.