How might I play the animation in this Shift to Sprint Script?

Yeah. Moving direction is NAN. I think magnitude should work though. Try it out.

So i’ve got this script right here:

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local Anim = characterHumanoid:WaitForChild("Animator"):LoadAnimation(animation)

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift and characterHumanoid.MoveDirection.Magnitude > 0 then
		characterHumanoid.WalkSpeed = boostedSpeed
		Anim:Play() -- To Play
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift or characterHumanoid.MoveDirection.Magnitude <= 0 then
		characterHumanoid.WalkSpeed = normalSpeed
		Anim:Stop()-- To Stop
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

However yet again, this breaks the script. It has not errors, but it doesn’t function.

Try this

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


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
Anim = character.characterHumanoid.Animator:LoadAnimation(animation)

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if characterHumanoid.MoveDirection.Magnitude > 0 then
			Anim:Play()
			characterHumanoid.WalkSpeed = boostedSpeed
		end

	
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.W then
		Anim:Stop()
		characterHumanoid.WalkSpeed = normalSpeed
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

If there are any errors please tell me because I haven’t tested this yet.

1 Like

It breaks the script sadly. I believe it might be because there is an if statement inside of an if statement. Testing the hopeful solution right now.

It did not function as I hoped, it still remained broken.

You can put as many if statements as you want. You can have else and put an if.

Do you get any errors? Did you put end?

Yes. I did do so, it still remained broken.

Can you try this now?

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


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local Humanoid = character:FindFirstChildWhichIsA("Humanoid")

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
Anim = Humanoid.Animator:LoadAnimation(animation)

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if Humanoid.MoveDirection.Magnitude > 0 then
			Anim:Play()
			Humanoid.WalkSpeed = boostedSpeed
		end

	
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Anim:Stop()
		Humanoid.WalkSpeed = normalSpeed
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

Again, any errors? At all? Search your script name in output. Even a warning is important especially if it’s an “Infinite yield possible on WaitForChild”

No errors, for some reason? It doesn’t show anything.

Then there’s no syntax error and an annoying error which doesn’t well you what’s wrong… Because there’s nothing wrong. Why don’t you have a set speed on the running speed by the way?

I had made a variable for the normal speed so I decided to multiply it instead of set it.

1 Like

I have solved the issue, thank you all for the help that you gave.

1 Like