Sprinting Script no working correctly

You can write your topic however you want, but you need to answer these questions:

  1. For my Sprint script to work as it should Keep it simple and clear!

  2. **When in Roblox studio you can press wasd then shift but in actual game when published and on roblox you have to press shift then wasd YOU HAVE TO otherwise you dont sprint ** Include screenshots / videos if possible!

  3. I’ve tried switching around code looking for answers But ive found none Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

What is the code for your sprint script?

local character = script.Parent
local humanoid = character:WaitForChild(“Humanoid”)
local UIS = game:GetService(“UserInputService”)
local tweenService = game:GetService(“TweenService”)
local Anim = Instance.new(‘Animation’)
local jumpingAnimation = Instance.new(‘Animation’)
local IdleAnimation = Instance.new(‘Animation’)
IdleAnimation.AnimationId = ‘rbxassetid://180435571’
jumpingAnimation.AnimationId = ‘rbxassetid://11309644149’
Anim.AnimationId = ‘rbxassetid://11309524430’
local PlayAnim = humanoid:LoadAnimation(Anim)
local playJumping = humanoid:LoadAnimation(jumpingAnimation)
local playIdle = humanoid:LoadAnimation(IdleAnimation)
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then return end

local leftShiftInput = input.KeyCode == Enum.KeyCode.LeftShift
local wKeyInput = input.KeyCode == Enum.KeyCode.W
local aKeyInput = input.KeyCode == Enum.KeyCode.A
local sKeyInput = input.KeyCode == Enum.KeyCode.S
local dKeyInput = input.KeyCode == Enum.KeyCode.D

local leftShift = UIS:IsKeyDown(Enum.KeyCode.LeftShift)
local wKey = UIS:IsKeyDown(Enum.KeyCode.W)
local aKey = UIS:IsKeyDown(Enum.KeyCode.A)
local sKey = UIS:IsKeyDown(Enum.KeyCode.S)
local dKey = UIS:IsKeyDown(Enum.KeyCode.D)



if leftShiftInput or wKeyInput or aKeyInput or sKeyInput or dKeyInput then
	if leftShift == true and humanoid.WalkSpeed ~= 31 then
		if wKey == true or aKey == true or sKey == true or dKey == true then
			local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
			local goals = {FieldOfView = 80}
			local camFOVTween = tweenService:Create(workspace.CurrentCamera, info, goals)
			wait()
			print("SPRINTING")
			camFOVTween:Play()
			PlayAnim:Play()
			humanoid.WalkSpeed = 31
			script.Speed.Value = humanoid.WalkSpeed	
		end	
	end
end	
if leftShiftInput or wKeyInput or aKeyInput or sKeyInput or dKeyInput then
	if wKey == true or aKey == true or sKey == true or dKey == true then
		if leftShift == true and humanoid.WalkSpeed ~= 31 then
			local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
			local goals = {FieldOfView = 80}
			local camFOVTween = tweenService:Create(workspace.CurrentCamera, info, goals)
			wait()
			print("SPRINTING")
			camFOVTween:Play()
			PlayAnim:Play()
			humanoid.WalkSpeed = 31
			script.Speed.Value = humanoid.WalkSpeed	
		end	
	end
end	

end)

UIS.InputEnded:Connect(function(input, isTyping)
if isTyping then return end

local leftShiftInput = input.KeyCode == Enum.KeyCode.LeftShift
local wKeyInput = input.KeyCode == Enum.KeyCode.W
local aKeyInput = input.KeyCode == Enum.KeyCode.A
local sKeyInput = input.KeyCode == Enum.KeyCode.S
local dKeyInput = input.KeyCode == Enum.KeyCode.D

local leftShift = UIS:IsKeyDown(Enum.KeyCode.LeftShift)
local wKey = UIS:IsKeyDown(Enum.KeyCode.W)
local aKey = UIS:IsKeyDown(Enum.KeyCode.A)
local sKey = UIS:IsKeyDown(Enum.KeyCode.S)
local dKey = UIS:IsKeyDown(Enum.KeyCode.D)

if leftShiftInput then
	if humanoid.WalkSpeed ~= 13 then
		local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
		local goals = {FieldOfView = 65}
		local camFOVTween = tweenService:Create(workspace.CurrentCamera, info, goals)
		wait()
		camFOVTween:Play()
		PlayAnim:Stop()
		humanoid.WalkSpeed = 13
		script.Speed.Value = humanoid.WalkSpeed
	end
	
elseif wKeyInput or aKeyInput or sKeyInput or dKeyInput then
	if wKey == false and aKey == false and sKey == false and dKey == false and humanoid.WalkSpeed ~= 13 then
		local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
		local goals = {FieldOfView = 65}
		local camFOVTween = tweenService:Create(workspace.CurrentCamera, info, goals)
		wait()
		camFOVTween:Play()
		PlayAnim:Stop()
		humanoid.WalkSpeed = 13
		script.Speed.Value = humanoid.WalkSpeed
	end									
	
end	
local jumpingAnimation = Instance.new('Animation')
jumpingAnimation.AnimationId = 'rbxassetid://11111313514'
local playJumping = humanoid:LoadAnimation(jumpingAnimation)
local keepJumpingAnimation = false
local FallingAnimation = Instance.new('Animation')
FallingAnimation.AnimationId = 'rbxassetid://11111303031'
local playFalling = humanoid:LoadAnimation(FallingAnimation)
local ClimbingAnimation = Instance.new('Animation')
ClimbingAnimation.AnimationId = 'rbxassetid://180436334'
local playClimbing = humanoid:LoadAnimation(ClimbingAnimation)
		local stateConnection = character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
			if (old == Enum.HumanoidStateType.Freefall) or (new == Enum.HumanoidStateType.Landed)then
				if Enum.KeyCode.LeftShift then
					PlayAnim:Play()
					if humanoid.MoveDirection.Magnitude == 0 then
						playIdle:Play()
						PlayAnim:Stop()
					end
					if humanoid.MoveDirection.Magnitude > 0 then
						playIdle:Stop()
						PlayAnim:Play()
					end
				elseif not Enum.KeyCode.LeftShift then
					PlayAnim:Stop()
					if humanoid.MoveDirection.Magnitude == 0 then
						playIdle:Play()
						PlayAnim:Stop()
					end
					if humanoid.MoveDirection.Magnitude > 0 then
						playIdle:Stop()
						PlayAnim:Play()
					end
				end
			end
		end)
	humanoid.Jumping:Connect(function()
		if PlayAnim.IsPlaying then
		PlayAnim:Stop()
			if keepJumpingAnimation == true then
			playJumping:Play()
			end	
		local stateConnection = character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
			if (old == Enum.HumanoidStateType.None) or (new == Enum.HumanoidStateType.Jumping)then
				if Enum.KeyCode.LeftShift then
					PlayAnim:Stop()
				end
				local stateConnection = character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
					if (old == Enum.HumanoidStateType.Freefall) or (new == Enum.HumanoidStateType.Landed)then
						if Enum.KeyCode.LeftShift then
							PlayAnim:Play()
							humanoid.WalkSpeed = 31
							script.Speed.Value = humanoid.WalkSpeed
						elseif not Enum.KeyCode.LeftShift then
							PlayAnim:Play()
							humanoid.WalkSpeed = 31
							script.Speed.Value = humanoid.WalkSpeed
						end
						if humanoid.MoveDirection.Magnitude == 0 then
							playIdle:Play()
							PlayAnim:Stop()
						end
					end	
				end)
			end
		end)	
	end		
end)	

end)

sorry i was trying get something

sorry its all spaghetti i was in a hurry to make it work with my bhopping system

you can come here and Copy it to save time:)

I tried the script out and it appeared to work fine. What exactly is the issue?

When you publish the game and play it on roblox you have to press shift first then and only then can you press wasd. if you press wasd then shift you dont sprint

I edited the code quite a bit. I found a couple memory leaks as well.
Anyways, try this out since I’m not really able to reproduce the issue:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local Anim = Instance.new('Animation')
local jumpingAnimation = Instance.new('Animation')
local IdleAnimation = Instance.new('Animation')
IdleAnimation.AnimationId = 'rbxassetid://180435571' 
jumpingAnimation.AnimationId = 'rbxassetid://11309644149' 
Anim.AnimationId = 'rbxassetid://11309524430' 
local PlayAnim = humanoid:LoadAnimation(Anim)
local playJumping = humanoid:LoadAnimation(jumpingAnimation)
local playIdle = humanoid:LoadAnimation(IdleAnimation)
UIS.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end

	local leftShiftInput = input.KeyCode == Enum.KeyCode.LeftShift
	--[[local wKeyInput = input.KeyCode == Enum.KeyCode.W
	local aKeyInput = input.KeyCode == Enum.KeyCode.A
	local sKeyInput = input.KeyCode == Enum.KeyCode.S
	local dKeyInput = input.KeyCode == Enum.KeyCode.D]]

	--[[local leftShift = UIS:IsKeyDown(Enum.KeyCode.LeftShift)
	local wKey = UIS:IsKeyDown(Enum.KeyCode.W)
	local aKey = UIS:IsKeyDown(Enum.KeyCode.A)
	local sKey = UIS:IsKeyDown(Enum.KeyCode.S)
	local dKey = UIS:IsKeyDown(Enum.KeyCode.D)]]



	if leftShiftInput then
		if humanoid.WalkSpeed ~= 31 then
			local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
			local goals = {FieldOfView = 80}
			local camFOVTween = tweenService:Create(workspace.CurrentCamera, info, goals)
			wait()
			print("SPRINTING")
			camFOVTween:Play()
			PlayAnim:Play()
			humanoid.WalkSpeed = 31
			script.Speed.Value = humanoid.WalkSpeed	
		end
	end
end)

local keepJumpingAnimation=false
UIS.InputEnded:Connect(function(input, isTyping)
	if isTyping then return end

	local leftShiftInput = input.KeyCode == Enum.KeyCode.LeftShift
	--[[local wKeyInput = input.KeyCode == Enum.KeyCode.W
	local aKeyInput = input.KeyCode == Enum.KeyCode.A
	local sKeyInput = input.KeyCode == Enum.KeyCode.S
	local dKeyInput = input.KeyCode == Enum.KeyCode.D]]

	--local leftShift = UIS:IsKeyDown(Enum.KeyCode.LeftShift)
	--[[local wKey = UIS:IsKeyDown(Enum.KeyCode.W)
	local aKey = UIS:IsKeyDown(Enum.KeyCode.A)
	local sKey = UIS:IsKeyDown(Enum.KeyCode.S)
	local dKey = UIS:IsKeyDown(Enum.KeyCode.D)]]

	if leftShiftInput then
		if humanoid.WalkSpeed ~= 13 then
			local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
			local goals = {FieldOfView = 65}
			local camFOVTween = tweenService:Create(workspace.CurrentCamera, info, goals)
			wait()
			camFOVTween:Play()
			PlayAnim:Stop()
			humanoid.WalkSpeed = 13
			script.Speed.Value = humanoid.WalkSpeed
		end
	end
	--[[local jumpingAnimation = Instance.new('Animation')
	jumpingAnimation.AnimationId = 'rbxassetid://11111313514'
	local playJumping = humanoid:LoadAnimation(jumpingAnimation)
	local FallingAnimation = Instance.new('Animation')
	FallingAnimation.AnimationId = 'rbxassetid://11111303031'
	local playFalling = humanoid:LoadAnimation(FallingAnimation)
	local ClimbingAnimation = Instance.new('Animation')
	ClimbingAnimation.AnimationId = 'rbxassetid://180436334'
	local playClimbing = humanoid:LoadAnimation(ClimbingAnimation)]]


end)

local stateConnection = character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
	if (old == Enum.HumanoidStateType.Freefall) or (new == Enum.HumanoidStateType.Landed)then
		PlayAnim:Play()
		if humanoid.MoveDirection.Magnitude == 0 then
			playIdle:Play()
			PlayAnim:Stop()
		end
		if humanoid.MoveDirection.Magnitude > 0 then
			playIdle:Stop()
			PlayAnim:Play()
		end
	elseif not Enum.KeyCode.LeftShift then
		PlayAnim:Stop()
		if humanoid.MoveDirection.Magnitude == 0 then
			playIdle:Play()
			PlayAnim:Stop()
		end
		if humanoid.MoveDirection.Magnitude > 0 then
			playIdle:Stop()
			PlayAnim:Play()
		end
	end
end)


humanoid.Jumping:Connect(function()
	if PlayAnim.IsPlaying then
		PlayAnim:Stop()
		if keepJumpingAnimation == true then
			playJumping:Play()
		end	
		local stateConnection = character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
			if (old == Enum.HumanoidStateType.None) or (new == Enum.HumanoidStateType.Jumping)then
				if Enum.KeyCode.LeftShift then
					PlayAnim:Stop()
				end
				local stateConnection = character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
					if (old == Enum.HumanoidStateType.Freefall) or (new == Enum.HumanoidStateType.Landed)then
						if Enum.KeyCode.LeftShift then
							PlayAnim:Play()
							humanoid.WalkSpeed = 31
							script.Speed.Value = humanoid.WalkSpeed
						elseif not Enum.KeyCode.LeftShift then
							PlayAnim:Play()
							humanoid.WalkSpeed = 31
							script.Speed.Value = humanoid.WalkSpeed
						end
						if humanoid.MoveDirection.Magnitude == 0 then
							playIdle:Play()
							PlayAnim:Stop()
						end
					end	
				end)
			end
		end)	
	end		
end)

Thank you for this immaculate peace of art, But I do have one Complaint one thing you forgot to think about was the fact that you can just press shift and the animation plays you dont even have to press wasd, but i fixed it with some true and false and while wait do’s But other than that this was Perfect and it worked fine :slight_smile:

1 Like