How would I keep an animation at 1 part? (help needed)

So I made a block animation for a sword, I want it so when they press F (while the tool is equipped), they start up the block animation, and then it stays at the last keyframes of the animation which shows them blocking until they let go of F.
https://gyazo.com/6f38772c0206b98eb8a3477377d429d6
I have also made a block walk, how would the game detect that the player is walking? It sounds very simple for you guys but I’m pretty new to scripting, any help?
https://gyazo.com/a2b9dd65fb61605f944246d6fefc2fc8

1 Like

Example code, would also be appreciated

i’d suggest making a simple “block idle” animation loop, in which the characters arms are just stationary in the blocking pose, you don’t need to stop the animation at the last keyframe, just play it and roblox automatically handles the transition from whatever animation > block animation, you can even change the transition time when you play the anim with the first argument of the function AnimationTrack:Play(transitionTime)

you don’t need a separate animation for walking when blocking, you can just make the block animation to affect only the arms and ignore the chest, hips and legs (meaning other animations like default run/walk can take over)
you just have to remove the joints from animation editor before exporting

Multiple animations are the most efficient way to do it.

  1. Block animation

You can use animationTrack:AdjustSpeed(0) to freeze an animation. To detect when to freeze it, I think you’ll need to create an animation event in the animation. Then you can use animationTrack:GetMarkerReachedSignal(eventName):Connect(freezeFunction) to freeze it at the correct time. For the input, you can use either UserInputService or ContextActionService.

This should now handle unequipping and equipping too. I haven’t tested it at all, though, so I’m not sure if it works. The code should be in a LocalScript in StarterPlayerScripts. Everytime you need to set the sword, use the setSword function. If the player needs to have more than one functional swords at the same time, some changes are still needed. This currently only supports one sword. You’ll need to have your own code that calls setSword and gives the sword tool as an argument, because I don’t know when you give the player the sword and how you get the tool to give.

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local BLOCK_ANIM = -- animation instance

local sword = -- use the setSword function when you need to set a sword, for example, when the player respawns.

local plr = Players.LocalPlayer

local blockAnimTrack
local equipConn, unEquipConn, inputBeganConn, inputEndedConn, markerConn
local blocking = false


local function onCharacterAdded(char)
    blocking = false
    local hum = char:WaitForChild("Humanoid")
    blockAnimTrack = hum:LoadAnimation(BLOCK_ANIM)
end

local function stopBlocking()
    blocking = false
    blockAnimTrack:Stop()
    inputEndedConn:Disconnect()
    markerConn:Disconnect()
    inputEndedConn, markerConn = nil, nil
end

local function inputEnded(input)
    if input.KeyCode == Enum.KeyCode.F then
        stopBlocking()
    end
end

local function freezeAnim()
    blockAnimTrack:AdjustSpeed(0)
end

local function block()
    blocking = true
    blockAnimTrack:Play()
    markerConn = blockAnimTrack:GetMarkerReachedSignal:Connect(freezeAnim)
    inputEndedConn = UserInputService.InputEnded:Connect(inputEnded)
end

local function inputBegan(input)
    if input.KeyCode == Enum.KeyCode.F then
        block()
    end
end

local function disableBlocking()
    if blocking then
        stopBlocking()
    end
    inputBeganConn:Disconnect()
end

local function equipped()
    inputBeganConn = UserInputService.InputBegan:Connect(inputBegan)
end

local function removeSwordReferences()
    disableBlocking()
    equipConn:Disconnect()
    unEquipConn:Disconnect()
    sword = nil
end

local function setSword(newSword)
    removeSwordReferences()
    sword = newSword
    equipConn = sword.Equipped:connect(equipped)
    unEquipConn = sword.Unequipped:Connect(disableBlocking)
end


plr.CharacterAdded:Connect(onCharacterAdded)
UserInputService.InputBegan:Connect(inputBegan)
  1. Block walk

You can use Humanoid.MoveDirection to see if the humanoid is moving or attempting to move and if it’s moving and the animation isn’t playing, play it. If it’s not moving and the animation is playing, don’t play it.

local function handleWalkAnim(hum, animTrack)
    if hum.MoveDirection ~= Vector3.new() and not animTrack.IsPlaying then
        animTrack:Play()
    elseif hum.MoveDirection == Vector3.new() and animTrack.IsPlaying then
        animTrack:Stop()
    end
end
3 Likes

Could I get an example for the Humanoid.MoveDirection?

Thanks, I’ll try it out once I get on studio.

For this, should I put an animation inside the script and just tell the script the path to that animation?

Could anyone help me with this question?

Apparently it isn’t working when I hold F, no animation plays. Here is the script

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local BLOCK_ANIM = Instance.new("Animation")
BLOCK_ANIM.AnimationId = "rbxassetid://5829956934"

local plr = game.Players.LocalPlayer

local blockAnimTrack
local inputEndedConn, markerConn


local function onCharacterAdded(char)
	local hum = char:WaitForChild("Humanoid")
	blockAnimTrack = hum:LoadAnimation(BLOCK_ANIM)
end

local function inputEnded(Input, gameProcessed)
	if Enum.KeyCode == Enum.KeyCode.F then
		if not gameProcessed then
		blockAnimTrack:Stop()
		inputEndedConn:Disconnect()
		markerConn:Disconnect()
		inputEndedConn, markerConn = nil, nil
	end
end

local function freezeAnim()
	blockAnimTrack:AdjustSpeed(0)
end

local function block()
	markerConn = blockAnimTrack:GetMarkerReachedSignal(block):Connect(freezeAnim)
	inputEndedConn = UserInputService.InputEnded:Connect(inputEnded)
end

local function inputBegan(Input, gameProcessed)
	if Enum.KeyCode == Enum.KeyCode.F then
		if not gameProcessed then
		block()
	end
end
script.Parent.Unequipped:Connect(function()
	BLOCK_ANIM:Stop()
end)

plr.CharacterAdded:Connect(onCharacterAdded)
UserInputService.InputBegan:Connect(inputBegan)
	end
	end

I forgot to add blockAnimTrack:Play() to the block function. I have now added it. I believe you also want the animations to only play when the sword is equipped. I have now edited the code to supports this. However, it only supports one sword. I also fixed some syntax.

1 Like