Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I pause the animation when it is done?

I am trying to make a gun, so whenever the gun is equipped it plays an animation.

local Gun = script.Parent
local Player = Gun.Parent.Parent
local Char = Player.Character

Gun.Equipped:Connect(function()
	print("Equipped gun!")
	local Motor6d = Instance.new("Motor6D", Char.RightHand)
	Motor6d.Name = "GunMotor6d"
	Motor6d.Part0 = Char.RightHand
	Motor6d.Part1 = Gun.Handle
	
	AnimTrack = Char.Humanoid:LoadAnimation(Gun.Aim)
	AnimTrack:Play()
	
end)

Gun.Unequipped:Connect(function()
	print("UnEquipped gun!")
	if Char.RightHand:FindFirstChild("GunMotor6d") then
		Char.RightHand.GunMotor6d:Destroy()
	end
	AnimTrack:Stop()
end)

The problem is that when the animation ends, the character goes back to the idle pose instead of staying in the position it ended. If you know how to fix this, please let me know. Thanks in advance.

2 answers

Set the AnimationTrack.Looped property to true, or set your animation to be looped when you are creating it.

From my knowledge, you cannot pause an animation (and make the character stay in that position) where you want it to.

3 comments

0

It dosen't work :(

0

I am trying to do it like this - https://www.youtube.com/watch?v=99FymB2b9t4

0

Message me on discord @ Brandon#4128. What I've said does work. I did this for one of my games.

If the animation has multiple keyframes, you'll have to do one of two things:

A. Use a KeyFrameMarker B. Do a hacky thing-thing

A is definitely more recommended but considering the sheer complexity of even how to insert a marker in MAS, let alone the default Roblox animator, you could just do the latter.

For A, use the GetMarkerReached signal, and upon the right marker being reached, pause the timeline:

local Connection;
Connection = AnimationTrack:GetMarkerReachedSignal("SomeMarkerIdentifier"):Connect(function(...)
	AnimationTrack:Stop();
end)

For B, you can use a RenderStepped loop (or implement it in a function) and stop at a certain point before the length:

function stopAtTime(AnimationTrack, Query) --Query is a value.
	local Event = Instance.new("BindableEvent");
	local Connection;
	Connection =game:GetService('RunService').RenderStepped:Connect(function(delta)
		if (not AnimationTrack or not AnimationTrack.IsPlaying or AnimationTrack.TimePosition >= Animation.Length - Query then
			Animation:AdjustSpeed(0) --There isn't a :Pause() function because Roblox is bad, so we prevent the AnimationTrack from continuing by setting the speed to 0.
			Event:Fire(); 
			Connection:Disconnect();
		end
	end);
	return Event;
end;

At the end, we use an event here so you can use stopAtTime(...).Event:Wait() or somethin' like that.

1 comment

0

Too lazy to edit, so, Query should be a small number, like 0.2.

This discussion is preserved as it was. New answers, comments, and votes are closed.