How to stop animation from going back to starting position?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Hello!, I’m trying to have my position for the enemy to end at the where the animation stops at.

  1. What is the issue? Include screenshots / videos if possible!

My issue is that my animation plays and then as soon as it’s finished, the enemy resets it’s position to the starting position which I do not want. I want the position for the enemy at end stay at the where the animation ends at.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried looking up things in the devforum related to this but I was unable to find any helpful solutions based on this. I’m a beginner scripter and my knowledge isn’t too great but examples help me out the best. Something dealing with the CFrame Position or Bodymovers? I just want the enemy’s position to stay where it ends at the end of my animation. I have provided a gif below showing what the issue looks like.
2024-08-2203-47-29-ezgif.com-video-to-gif-converter

Ill list some steps and give you the example code later

  1. Get the animation clip of your animation using AnimationClipProvider:GetAnimationClipAsync(), so you can get the keyframes in your animation
  2. Use :GetKeyframes to get the last keyframe
  3. Get the poses of the last keyframe to apply to all motor6d’s in the enemy
    Final code;
local APS = game:GetService('AnimationClipProvider')
local animation = -- Your animation here
local animationclip = APS:GetAnimationClipAsync(animation.AnimationId)
local animationtrack = -- Your animation track here
animationtrack:Play()
-- Keep them at ending position
animation.Stopped:Connect(function()
local lastkeyframe = animationclip:GetKeyframes()[#animationclip:GetKeyframes()]
local poses = lastkeyframe:GetPoses()
for _, pose in ipairs(poses) do
   for name, transform in pairs(pose) do
       local motor : Motor6D = enemy:FirstFirstChild(name, true)
       local transform = CFrame.new(transform.Position,transform.Rotation)
       motor.Transform = transform
   end
end
end)

It took 2h to compile this, hope this helps! (Expect bugs since I didn’t test this)

too complicated

local animationTrack = ...

animationTrack:Play()
task.delay(animationTrack.Length - 0.01, function()
    animationTrack:AdjustSpeed(0)
end)
1 Like

Uhh, where would I put this exactly? Do I make a new server script or do I put it in the server script that does the animation?

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local GrabRemote = RS.Grab
local ragdoll = require(game:GetService("ReplicatedStorage").Ragdoll)

local Anims = script:WaitForChild("Anims")
GrabRemote.OnServerEvent:Connect(function(plr, victim)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char:FindFirstChild("Humanoid")
	local hrp = char:FindFirstChild("HumanoidRootPart")
	for i,v in pairs (workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and (char.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude <=5 and v.Name ~= char.Name then

	local enemycharacter = v
	ragdoll:rig(enemycharacter) -- It's necessary for temporary ragdoll's
	ragdoll:initTimeStamps(enemycharacter) -- Initialize time stamps for the character)
	if victim then
		local ehum = victim:FindFirstChild("Humanoid")
		local ehrp = victim:FindFirstChild("HumanoidRootPart")
		
		
		if ehum and hum then
			hrp.CFrame = CFrame.new(hrp.Position,Vector3.new(ehrp.Position.X,hrp.Position.Y,ehrp.Position.Z))
			ehrp.CFrame = hrp.CFrame * CFrame.new(0,0,-2)
			ehrp.CFrame = CFrame.new(ehrp.Position,Vector3.new(hrp.Position.X,ehrp.Position.Y,hrp.Position.Z))
			
				v:PivotTo(char.HumanoidRootPart.CFrame * CFrame.new(0,0,-3))
				v:PivotTo(v:GetPivot() * CFrame.Angles(0, math.rad(-180),0))
			
			local mainanim = Anims:WaitForChild("Main")
			local victimanim = Anims:WaitForChild("Enemy")

			local mainplay = hum:LoadAnimation(mainanim)
			local victimplay = ehum:LoadAnimation(victimanim)

			mainplay:Play()
			victimplay:Play()
				end
			end
		end
	end
end)

The one with the animation
Does the non-complicated one work? It seems legit