Animation runs in place

  1. What do you want to achieve? I want the animation to start when the player moves while holding shift.

  2. What is the issue? The issue is that when you hold the shift, the animation just starts.

  3. What solutions have you tried so far? I tried looking for answers in the DevForum, but I couldn´t find anything.
    Here is a video:

and here is the script:

local char = plr.Character
local humanoid = char:FindFirstChild("Humanoid")
local hrp = char:FindFirstChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")

local RA = Instance.new("Animation")
RA.AnimationId = "rbxassetid://12733397822"
local playRA = humanoid:LoadAnimation(RA)

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 24
		playRA:Play()
	end
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 16
		playRA:Stop()
	end
end)

Thank you!

-- top of script
local isRunning = false
local minRunningSpeed = 0 -- change this if you want

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 24
		isRunning = true
	end
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 16
		isRunning = false
	end
end)

local function onRunning(speed)
	if speed > minRunningSpeed and isRunning then
		playRA:Play()
	else
		playRA:Stop()
	end
end

humanoid.Running:Connect(onRunning)
2 Likes

if key.KeyCode == Enum.KeyCode.LeftShift and Enum.KeyCode.W then

(So they have to be walking for the animation to play)

or try something like this v

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.LeftShift then
				humanoid.WalkSpeed = 24
				playRA:Play()
			end
		end)
	end
end)
1 Like

This has the problem of listening to an input multiple times, since you are not using :Disconnect() on the event, also, you cannot press shift before walking, and lastly this uses a lot of nesting, which is not a preferred practice.

1 Like

The script works! But can you explain how the script works? Im curious…

What if they are pressing ASD? It wouldn’t run

1 Like

Defines the Variables

Makes the speed change and changes isRunning to true

Does the opposite

Thsi function checks if the speed is higher than the minRunningSpeed, and the Player is Running, and plays the animation, if not it doesn’t

Connects the function

1 Like

Why is “isRunning” equaled to false?

Because at first the player isn’t running, so it’s false

1 Like

So then this means that if the humanoids walkspeed is higher than “minRunningSpeed” and isn´t running then play animation else stop animation or?

So, thai mean taht if the player is pressing leftShift, but is not moving, it will not make the Player run and stop the animation, if he’s pressing LeftShift and he’s moving he will run, and play the animation, clear now?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.