How would I hold an animation when a tool is in its Activated state?

  1. What do you want to achieve?
    I want my localscript to “hold” an animation when a tool’s Activated event fires, likened to that of a left click to hold an animation.

  2. What is the issue?
    Since I am relatively foreign to animation, I am not sure how I would implement this. I have established a KeyFrame reached event but to no avail. Please bear with me as I am also in my 4th week of scripting

  3. What solutions have you tried so far?
    I have looked for several alternatives on the DevForum, but no joy. I am hoping that here in this article, someone willing could articulate my issues and address them.

Code:

local player = game:GetService("Players").LocalPlayer
local character = ((player.Character and player.Character.Parent) and player.Character) or player.CharacterAdded:Wait()
local Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
local Soda_Animation = script.Parent:FindFirstChild("Animation")
local Soda_Track = Animator:LoadAnimation(Soda_Animation)

local drinking = true

script.Parent.Activated:Connect(function()
	if drinking then
		drinking = false
		Soda_Track:Play()
		Soda_Track.KeyframeReached:Connect(function(keyframeName) --KeyframeReached event doesn't fire
			print("hello1")
			if keyframeName == "KeyFrame2" then
				print("hello")
				Soda_Track:AdjustSpeed(0)
			end
		end)
	end
end)

script.Parent.Deactivated:Connect(function()
	Soda_Track:Stop()
	drinking = true
end)```

I’m presuming you want it to just hold the arm in a drinking position, it would be easier if you just set the animation to looped and then had a delay till the first keyframe by how fast you want it to transition to the drinking pose, this way itll seamlessly transition as it currently is and then will stay in that pose until you :Stop()

I’m sorry, I can’t seem to visualize what this would be like, do you mind elaborating with a code sample?

image
and remove the keyframe reached segment

I think I understand what you mean now, so to elaborate in a framework:

AnimationTrack:
Delay > DrinkKeyFrame

So to clarify, this will “hold” at the DrinkKeyFrame when AT.Looped = true ?

since the cframes are already at the designated position nothing would change when the animation runs the 2nd 3rd 4th time since it’s already in the applied pose.

1 Like

Works great, thank you so much!