Shift to sprint script not working

Im making a shift to sprint script with a dynamic camera + stamina bar however Im struggling to make the script. No visible errors in the output and the player’s speed doesn’t change and the tween doesn’t work.

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer

local ts = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
local PlayerGui = plr.PlayerGui

local StaminaBar = PlayerGui:WaitForChild("ScreenVisuals").StaminaBar.Fill

local Power = 10

local maxWalkSpeed = 18
local minWalkspeed = 13

function Lerp(min, max, pos)
	return min + (max - min) * pos
end

function Update(hum)
	local goal = {}
	goal.FieldOfView = Lerp(30, 70, hum.WalkSpeed / maxWalkSpeed)
	local tweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Quart)
	local tween = ts:Create(cam, tweenInfo, goal)
	tween:Play()
end

plr.CharacterAdded:Connect(function(char)
	local hum = char:WaitForChild("Humanoid")
	Update(hum)
	hum:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
		Update(hum)
	end)
end)

uis.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed then
		plr.Character.Humanoid.WalkSpeed = maxWalkSpeed
		while Power > 0 do
			Power = Power - .03
			StaminaBar:TweenSize(UDim2.new(Power/ 0, 341, 0, 10), 'Out', 'Quint', .1, true)
			wait()
			if Power <= 0 then
				plr.Character.Humanoid.WalkSpeed = minWalkspeed
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed then
		plr.Character.Humanoid.WalkSpeed = minWalkspeed
		while Power < 10 do
			Power = Power + .03
			StaminaBar:TweenSize(UDim2.new(Power/ 0, 341, 0, 10), 'Out', 'Quint', .1, true)
			wait()
			if Power <= 0 then
				plr.Character.Humanoid.WalkSpeed = minWalkspeed
			end
		end
	end
end)
1 Like

Your if statements under the UserInputService functions if input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed basically checks if the player is currently typing in the chat or a TextBox, and if they are, then the rest of the code will run, which is what is causing this issue.

Here is your updated code:

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer

local ts = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
local PlayerGui = plr.PlayerGui

local StaminaBar = PlayerGui:WaitForChild("ScreenVisuals").StaminaBar.Fill

local Power = 10

local maxWalkSpeed = 18
local minWalkspeed = 13

function Lerp(min, max, pos)
	return min + (max - min) * pos
end

function Update(hum)
	local goal = {}
	goal.FieldOfView = Lerp(30, 70, hum.WalkSpeed / maxWalkSpeed)
	local tweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Quart)
	local tween = ts:Create(cam, tweenInfo, goal)
	tween:Play()
end

plr.CharacterAdded:Connect(function(char)
	local hum = char:WaitForChild("Humanoid")
	Update(hum)
	hum:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
		Update(hum)
	end)
end)

uis.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then -- If player is NOT typing
		plr.Character.Humanoid.WalkSpeed = maxWalkSpeed
		while Power > 0 do
			Power = Power - .03
			StaminaBar:TweenSize(UDim2.new(Power/ 0, 341, 0, 10), 'Out', 'Quint', .1, true)
			wait()
			if Power <= 0 then
				plr.Character.Humanoid.WalkSpeed = minWalkspeed
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then -- if player is NOT typing
		plr.Character.Humanoid.WalkSpeed = minWalkspeed
		while Power < 10 do
			Power = Power + .03
			StaminaBar:TweenSize(UDim2.new(Power/ 0, 341, 0, 10), 'Out', 'Quint', .1, true)
			wait()
			if Power <= 0 then
				plr.Character.Humanoid.WalkSpeed = minWalkspeed
			end
		end
	end
end)
3 Likes

I don’t necessarily need to bump this topic, but it has been a few days without any update to this topic. So I am just wondering if you have solved your issue yet. Please let me know.

1 Like

Sorry sorry sorry I completely forgot about this, I found an alternative free shift to sprint script on the devforum:

UIS.InputBegan:Connect(function(key, processed)
	print("b")
	if key.UserInputType == Enum.UserInputType.Keyboard then
		print("key")
		if key.KeyCode == Enum.KeyCode[Keybind] and StaminaCount > 0 then
			print("a")
			StaminaUsed.Value = true
		end
	end
end)
UIS.InputEnded:Connect(function(key, processed)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode[Keybind] then
			print("b")
			StaminaUsed.Value = false
		end
	end
end)

StaminaUsed.Changed:Connect(function()
	if StaminaUsed.Value == true then
		Humanoid.WalkSpeed = SprintWalkspeed
		repeat
			wait(0.1)
			if StaminaCount > 0 then
				StaminaCount = StaminaCount - .5
				--Bar.Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)
				TweenService:Create(Bar,tweenInfo, {Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)}):Play()
				--Info.Text = StaminaCount.. "/" ..MaxStamina
			end
		until
		StaminaCount <= 0 or StaminaUsed.Value == false
		Humanoid.WalkSpeed = NormalWalkspeed
	elseif StaminaUsed.Value == false then
		Humanoid.WalkSpeed = NormalWalkspeed
		repeat
			wait(0.1)
			if StaminaCount < MaxStamina then
				StaminaCount = StaminaCount + .2
				--Bar.Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)
				TweenService:Create(Bar,tweenInfo, {Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)}):Play()
				--Info.Text = StaminaCount.. "/" ..MaxStamina
			end
		until
		StaminaCount == MaxStamina or StaminaUsed.Value == true
	end
end)

I got some bugs with it that im unsure on how to fix, its up to you if you’d like to help.

Bugs:

  • the input began function isnt working
  • the bar goes out of frame for some reason when it works (Can’t get a picture because it only happens when the script is working and rn its not)
1 Like

Can’t you just use this simple script?

game:GetService("UserInputService").InputBegan:Connect(function(input, process)
if process then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
humanoid.walkspeed = 20
end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input, process)
if process then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
humanoid.walkspeed = 16
end
end)
2 Likes

Is the bar parented to an invisible frame? (It should be)

2 Likes

This doesn’t appear to be the entire code. If possible, could you please show your entire code? Are you receiving any errors in the output?
Also, one thing to mention is that you should either use the coroutine.wrap or task.spawn functions for the StaminaUsed section of the code, to avoid yielding the entire script.

1 Like

I have tried to learn coroutines before and they didn’t really work out and I pretty much forgot how to use them. There is no visible errors in the output, as you can see I tried debugging in the InputBegan function and none of the print statements show in the output.

Script:

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

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

local Humanoid = Character:WaitForChild("Humanoid" ,5)
local StaminaUsed = script:WaitForChild("StaminaUsed" ,5)

local ItemProperties = require(ReplicatedStorage.Modules.ItemProperties)
local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))

local Cam = workspace.CurrentCamera
local Bar = script.Parent.Fill

local ItemProperties = require(ReplicatedStorage.Modules.ItemProperties)

local tweenInfo = TweenInfo.new(0.1)

local StaminaBoosters = {
	"BloxyCola"
}

function Lerp(min, max, pos)
	return min + (max - min) * pos
end


--SETTINGS
local Keybind = "LeftShift"
local MaxStamina = 100
local NormalWalkspeed = 16
local SprintWalkspeed = 25

local StaminaCount = MaxStamina

function Update(hum)
	local goal = {}
	goal.FieldOfView = Lerp(30, 70, hum.WalkSpeed / SprintWalkspeed)
	local tweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Quart)
	local tween = TweenService:Create(Cam, tweenInfo, goal)
	tween:Play()
end

Player.CharacterAdded:Connect(function(char)
	local Humanoid = char:WaitForChild("Humanoid")
	Update()
	StaminaUsed.Changed:Connect(function()
		print("update")
		Update()
	end)
end)

UIS.InputBegan:Connect(function(key, processed)
	print("b")
	if key.UserInputType == Enum.UserInputType.Keyboard then
		print("key")
		if key.KeyCode == Enum.KeyCode[Keybind] and StaminaCount > 0 then
			print("a")
			StaminaUsed.Value = true
		end
	end
end)
UIS.InputEnded:Connect(function(key, processed)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode[Keybind] then
			print("b")
			StaminaUsed.Value = false
		end
	end
end)

StaminaUsed.Changed:Connect(function()
	if StaminaUsed.Value == true then
		Humanoid.WalkSpeed = SprintWalkspeed
		repeat
			wait(0.1)
			if StaminaCount > 0 then
				StaminaCount = StaminaCount - .5
				--Bar.Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)
				TweenService:Create(Bar,tweenInfo, {Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)}):Play()
				--Info.Text = StaminaCount.. "/" ..MaxStamina
			end
		until
		StaminaCount <= 0 or StaminaUsed.Value == false
		Humanoid.WalkSpeed = NormalWalkspeed
	elseif StaminaUsed.Value == false then
		Humanoid.WalkSpeed = NormalWalkspeed
		repeat
			wait(0.1)
			if StaminaCount < MaxStamina then
				StaminaCount = StaminaCount + .2
				--Bar.Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)
				TweenService:Create(Bar,tweenInfo, {Size = UDim2.new(StaminaCount / MaxStamina, 0, .5, 0)}):Play()
				--Info.Text = StaminaCount.. "/" ..MaxStamina
			end
		until
		StaminaCount == MaxStamina or StaminaUsed.Value == true
	end
end)

1 Like

Oh right I did not know that, Thanks!

1 Like