I can't create Smooth Sprint with LeftShift

Can’t detect humanoid of player to change walk speed

  1. I want to smoothly ramp up speed one time for the player when LeftShift Started Input, and slow down speed one time when LeftShift Ended Input.

  2. I can’t figure out how to detect the player’s humanoid. I tried to make smooth walk speed change by using TweenService. Every time this gives only this error -
    image

Here is my code

local InputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local PlayerService = game:GetService("Players")

local Player = game.Players.LocalPlayer.Character
local Hum = Player.Humanoid

local SprintValue = 22
local WalkValue = 12

local goalSprint = {}
goalSprint.WalkSpeed = SprintValue
local goalWalk = {}
goalWalk.WalkSpeed = WalkValue

local tweenInfo = TweenInfo.new(
	3,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	1,
	false,
	0
)

local SprintTween = TweenService:Create(Hum, tweenInfo, goalSprint)
local WalkTween = TweenService:Create(Hum, tweenInfo, goalWalk)

InputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		SprintTween:Play()
	end
end)

InputService.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		WalkTween:Play()
	end
end)

If it possible to change speed smoothky without using TweenService, please let me know.

4 Likes

The character is nil. As you try to get it before the char loads in. Which is why this is happening. To fix this, put the script in startercharacterscripts

1 Like
local InputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local PlayerService = game:GetService("Players")

local Player = PlayerService.LocalPlayer.Character or PlayerService.LocalPlayer.CharacterAdded:Wait()
local Hum = Player:WaitForChild("Humanoid")

local SprintValue = 22
local WalkValue = 12

local goalSprint = {}
goalSprint.WalkSpeed = SprintValue
local goalWalk = {}
goalWalk.WalkSpeed = WalkValue

local tweenInfo = TweenInfo.new(
	3,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	1,
	false,
	0
)

local SprintTween = TweenService:Create(Hum, tweenInfo, goalSprint)
local WalkTween = TweenService:Create(Hum, tweenInfo, goalWalk)

InputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		SprintTween:Play()
	end
end)

InputService.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		WalkTween:Play()
	end
end)
3 Likes

just a warning, this will work the first time, if the character dies it wont again. The best way is

A putting it in a .CharacterAddedf
B moving the script to starter character scripts

Wrong. It’ll work just fine, since it has the CharacterAdded:Wait() part.

Your suggestion isn’t wrong, but it’s a little more of a long process, where it can be done just in 1 line.

if the script is in StarterCharacterScripts, then it would work even when the player dies. But because it’s at the beginning of the script, when the player dies, the old character model in your script wont get the new character model.

The best way to fix this is putting it in a .CharacterAdded

OR move script into StarterCharacterScripts

Humanoid Detection error is gone, but script does nothing. I think i did something wrong with Tweens. Now im looking for mistake in Tweens, if i will fix it i will end this topic. Thanks for help!

try doing this:

local InputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local PlayerService = game:GetService("Players")

local Player = game.Players.LocalPlayer.Character
local Hum = Player.Humanoid

local SprintValue = 22
local WalkValue = 12

local goalSprint = {
WalkSpeed = SprintValue
}
goalSprint.WalkSpeed = SprintValue
local goalWalk = {
WalkSpeed = WalkValue
}

local tweenInfo = TweenInfo.new(
	3,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	1,
	false,
	0
)

local SprintTween = TweenService:Create(Hum, tweenInfo, goalSprint)
local WalkTween = TweenService:Create(Hum, tweenInfo, goalWalk)

InputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		SprintTween:Play()
	end
end)

InputService.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		WalkTween:Play()
	end
end)

Fixed it, here’s your code. (Make sure to put in StarterCharacterScripts)

local InputService = game:GetService("UserInputService")

local Player = script.Parent;
local Hum = Player.Humanoid

local SprintValue = 22
local WalkValue = 12

local goalSprint = {
	WalkSpeed = SprintValue
}
goalSprint.WalkSpeed = SprintValue

local goalWalk = {
	WalkSpeed = WalkValue
}

local tweenInfo = TweenInfo.new(
	3,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	1,
	false,
	0
)

InputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		print("yes")
		for i = Hum.WalkSpeed, SprintValue, 1 do
			task.wait(0.05)
			Hum.WalkSpeed = i
		end
	end
end)

InputService.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		print("no")
		for i = Hum.WalkSpeed, WalkValue, -1 do
			task.wait(0.05)
			Hum.WalkSpeed = i
		end
	end
end)

Used for loops.

1 Like

If you made it with “for i”, i think there is no use for Tween in this script

local InputService = game:GetService("UserInputService")

local Player = script.Parent;
local Hum = Player.Humanoid

local SprintValue = 22
local WalkValue = 12

InputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		print("yes")
		for i = Hum.WalkSpeed, SprintValue, 1 do
			task.wait(0.05)
			Hum.WalkSpeed = i
		end
	end
end)

InputService.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		print("no")
		for i = Hum.WalkSpeed, WalkValue, -1 do
			task.wait(0.05)
			Hum.WalkSpeed = i
		end
	end
end)

Yeah I removed all the tween stuff (might have missed some but alr)

Made your script bettter and easier to read. It also tweens better.

local InputService = game:GetService("UserInputService")
local Player = script.Parent;
local Hum = Player.Humanoid

local SMOOTHNESS = 30

local SprintValue = 22
local WalkValue = 12
local Sprinting = false

InputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		print("yes")
		Sprinting = true
	end
end)

InputService.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		print("no")
		Sprinting = false
	end
end)

while wait() do
    if Sprinting then
          Hum.WalkSpeed += (SprintValue - Hum.WalkSpeed)/SMOOTHNESS 
    else
          Hum.WalkSpeed += (WalkValue - Hum.WalkSpeed)/SMOOTHNESS 
    end
end

The higher the SMOOTHNESS, the longer you will have to wait for the tween.

2 Likes

You forgot about this at first

local InputService = game:GetService("UserInputService")

but anyway your way to solve problem is great! Thanks everyone for your help!

1 Like
local Tweens = game:GetService("TweenService")
local UserInput = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local function OnInputBegan(StartKey)
	local Connection
	local function OnInputEnded(EndKey)
		if EndKey.KeyCode.Name:match("Shift$") then
			if StartKey == EndKey then
				local Tween = Tweens:Create(Humanoid, TweenInfo.new(1, Enum.EasingStyle.Linear), {WalkSpeed = 16})
				Tween:Play()
				Connection:Disconnect()
				Connection = nil
			end
		end
	end
	
	if StartKey.KeyCode.Name:match("Shift$") then
		local Tween = Tweens:Create(Humanoid, TweenInfo.new(1, Enum.EasingStyle.Linear), {WalkSpeed = 24})
		Tween:Play()
		Connection = UserInput.InputEnded:Connect(OnInputEnded)
	end
end

UserInput.InputBegan:Connect(OnInputBegan)

TweenService seems like a suitable use case for this.

2 Likes