I need help with animation

hello,
so i am making a melee weapon and when the swing animation finishes i want to switch it to the idle animation.

The snippet of code:

local humanoid = plr.Character:FindFirstChildWhichIsA("Humanoid")
		local animsList = anims:GetChildren()
		local randomAnim = humanoid:LoadAnimation(animsList[math.random(1, #animsList)])
		randomAnim:Play()
		hitbox:HitStart()
		local pl = humanoid:GetPlayingAnimationTracks()
		for i, g in pl do
			if g.Name == "Idle" then
				g:Stop()
			end
		end
		randomAnim.Ended:Connect(function()
			if isEquipped() then
				local idleAnimation = humanoid:LoadAnimation(tool.IdleAnims.Idle)
				idleAnimation:Play()
			end
		end)

What is the issue? Is it that i need to make it another anim priority?
But whats the issue? Well this: https://gyazo.com/1f5029cc27f4044aa9b820e91c830f3c
It finishes the anim, then wants to go to the default animation then snaps back to my idle animation, why is that?
(yes i tried: idleAnimation:Play(0) )

the animation snaps back probably because the animation takes some time to load, I haven’t tested yet, but try having a rig playing the animation in somewhere in workspace so the animation is already loaded.

why would i load a differen anim in a different rig? I can maybe load it before the anim is done

Instead of .Ended try .Stopped

sorry, I haven’t tested it yet but it might work, that could also work as well I presume

Putting .Stopped and loading it before works :+1:
Thanks to you all

Instead of marking your own answer as the solution, maybe mark @epic_tank 's post because he helped you??

I missclicked bro, thanks for letting me know

It just looked like you were greedy for solutions, luckily I was wrong. Thanks for being honest!