Anyone know how to fix this Shift to Sprint script?

No Errors

local UIS = game:GetService("UserInputService")

local Humanoid = script.Parent:WaitForChild("Humanoid")

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local RunAnim = Instance.new("Animation")
RunAnim.AnimationId = "rbxassetid://9530567165"
local animationTrack = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(RunAnim)

local fallAnim = Instance.new("Animation")
fallAnim.AnimationId = "rbxassetid://9535536699"
local fallAnimTrack = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(fallAnim)

UIS.InputBegan:Connect(function(input, gameProcessed) -- starts sprinting
	if not gameProcessed then
		Humanoid.Running:Connect(function(speed)
			if speed ~= 0 and input.KeyCode == Enum.KeyCode.LeftShift then
				if not Humanoid.FreeFalling then
					Humanoid.WalkSpeed = 25
					
					animationTrack:Play()
				end
			else
				if Humanoid.FreeFalling then
					animationTrack:Stop()
				end
			end
		end)
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessed) -- no longer starts sprinting
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			Humanoid.WalkSpeed = 16
			animationTrack:Stop()
		end
	end
end)
2 Likes

Could you please explain whats wrong with that like what it supposed to do and etc

1 Like

try this

local UIS = game:GetService("UserInputService")

local Humanoid = script.Parent:WaitForChild("Humanoid")

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local RunAnim = Instance.new("Animation")
RunAnim.AnimationId = "rbxassetid://9530567165"
local animationTrack = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(RunAnim)

local fallAnim = Instance.new("Animation")
fallAnim.AnimationId = "rbxassetid://9535536699"
local fallAnimTrack = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(fallAnim)

UIS.InputBegan:Connect(function(input, gameProcessed) -- starts sprinting
	if not gameProcessed then
		if speed ~= 0 and input.KeyCode == Enum.KeyCode.LeftShift then
			if not Humanoid.FreeFalling then
				Humanoid.WalkSpeed = 25
				animationTrack:Play()
			end
		else
			if Humanoid.FreeFalling then
				animationTrack:Stop()
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessed) -- no longer starts sprinting
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			Humanoid.WalkSpeed = 16
			animationTrack:Stop()
		end
	end
end)
1 Like

Why are you doing

script.Parent:WaitForChild("Humanoid")

when you’re loading the animations when you’ve already referenced the Humanoid? Also don’t use the Humanoid.Running event inside of here, however, if you HAVE to then consider making a variable which detects if you pressed a key or not i.e.

local HoldingLeftShit = UIS:IsKeyDown(Enum.KeyCode.LeftShift)

And reference it when the humanoid starts running like;

Humanoid.Running:Connect(function(speed) 
    if UIS:IsKeyDown(Enum.KeyCode.LeftShift then
        -- do stuff
    else
        -- dont do stuff
    end
end)

or just use a regular UIS.InputBegan script which detects when you’re holding down shift and increases your speed and UIS.InputEnded which @SharkBloxYT above already did

1 Like

Before giving me code, please explain to me what you’ve changed.

and also, I said in the title what the script does

He removed the Humanoid.Running event which was most likely to be causing the script to not work

Ye that what I removed from the script

Still doesn’t seem to work D:

Try this :point_down::point_down::point_down:

local UIS = game:GetService("UserInputService")

local Humanoid = script.Parent:WaitForChild("Humanoid")

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local RunAnim = Instance.new("Animation")
RunAnim.AnimationId = "rbxassetid://9530567165"
local animationTrack = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(RunAnim)

local fallAnim = Instance.new("Animation")
fallAnim.AnimationId = "rbxassetid://9535536699"
local fallAnimTrack = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(fallAnim)

UIS.InputBegan:Connect(function(input, gameProcessed) -- starts sprinting
if input.UserInputType == Enum.UserInputType.Keyboard then
	if input.KeyCode == Enum.KeyCode.LeftShift then
			if speed ~= 0 then
				if not Humanoid.FreeFalling then
					Humanoid.WalkSpeed = 25
					
					animationTrack:Play()
				end
			else
				if Humanoid.FreeFalling then
					animationTrack:Stop()
				end
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input2, gameProcessed2) -- no longer starts sprinting
			Humanoid.WalkSpeed = 16
			animationTrack:Stop()
end)

Whenever the player presses a key then it will detect if it is LeftShift and then the player should sprint.