Small bug with Sprinting System

Alright so, I made this sprinting system a while ago and I don’t really know how to fix this issue?

Whenever you hold down Left Ctrl first and then hold down a movement key (W, A, S, or D) to move, it’ll just not let the player move at all. Note that I made this sprint system a while ago when I was still trying to learn. Anyways, does anyone know how to fix this bug?

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")

local PlayerGui = Player:WaitForChild("PlayerGui")
local StaminaGui = PlayerGui:WaitForChild("StaminaGui")

local StaminaOuterFrame = StaminaGui:WaitForChild("StaminaOuterFrame")
local StaminaInnerFrame = StaminaOuterFrame:WaitForChild("StaminaInnerFrame")

local StaminaText = StaminaInnerFrame:WaitForChild("StaminaText")
local Energy = script:WaitForChild("Energy")

local Rate = 0.05
local isRunning = false

local PlayerCamera = game.Workspace.CurrentCamera
local OriginalFOV = 70
local SprintingFOV = 80

local function UpdateBar()
	while true do
		StaminaText.Text = Energy.Value
		TweenService:Create(StaminaInnerFrame, TweenInfo.new(0.5 ,Enum.EasingStyle.Linear), {Size = UDim2.new(Energy.Value / 100, 0, 1, 0)}):Play()
		task.wait(0.25)
	end
end
2 Likes

Is this the full code because I don’t see any part changing speed

2 Likes

wait hold on I think I messed up something

2 Likes

alr sending full code (I forgot the rest of the code) sorry

2 Likes

Here is the full code!

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")

local PlayerGui = Player:WaitForChild("PlayerGui")
local StaminaGui = PlayerGui:WaitForChild("StaminaGui")

local StaminaOuterFrame = StaminaGui:WaitForChild("StaminaOuterFrame")
local StaminaInnerFrame = StaminaOuterFrame:WaitForChild("StaminaInnerFrame")

local StaminaText = StaminaInnerFrame:WaitForChild("StaminaText")
local Energy = script:WaitForChild("Energy")

local Rate = 0.05
local isRunning = false

local PlayerCamera = game.Workspace.CurrentCamera
local OriginalFOV = 70
local SprintingFOV = 80

local function UpdateBar()
	while true do
		StaminaText.Text = Energy.Value
		TweenService:Create(StaminaInnerFrame, TweenInfo.new(0.5 ,Enum.EasingStyle.Linear), {Size = UDim2.new(Energy.Value / 100, 0, 1, 0)}):Play()
		task.wait(0.25)
	end
end

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if Energy.Value > 10 then
			isRunning = true
			TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 25}):Play()
			TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = SprintingFOV}):Play()
			while isRunning == true do
				if isRunning == false then
					TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
					break
				elseif Energy.Value <= 10 then
					if HRP:FindFirstChild("Speed Sparkles") then
						TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 20}):Play()
						TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
						break
					else
						TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 16}):Play()
						break
					end
				else
					Energy.Value -= 1
					task.wait(Rate)
				end
			end
		else
			return
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.LeftControl then
		isRunning = false
		if HRP:FindFirstChild("Speed Sparkles") then
			TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 20}):Play()
			TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
		else
			TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 16}):Play()
			TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
		end
		while isRunning == false do
			if isRunning == true or Energy.Value >= 100 then break end
			Energy.Value += 1
			task.wait(Rate)
		end
	end
end)
task.spawn(UpdateBar)
2 Likes

Hello? Are you there? Where did you go?

2 Likes

can’t seem to find where the issue is,

2 Likes

@ItzBloxyDev Don’t waste peoples time.

There was absolutely nothing wrong with your code. New Roblox updates make CTRL + WASD sort through the Explorer (Who thought we needed this?)

Although nothings wrong with it. I did apply a fix in this code to make it use Shift instead of CTRL when in studio and I moved some stuff around to remove some code lines for better readability and Script Size.

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")

local PlayerGui = Player:WaitForChild("PlayerGui")
local StaminaGui = PlayerGui:WaitForChild("StaminaGui")

local StaminaOuterFrame = StaminaGui:WaitForChild("StaminaOuterFrame")
local StaminaInnerFrame = StaminaOuterFrame:WaitForChild("StaminaInnerFrame")

local StaminaText = StaminaInnerFrame:WaitForChild("StaminaText")
local Energy = script:WaitForChild("Energy")

local Rate = 0.05
local isRunning = false

local ManStudioSucks = game:GetService("RunService"):IsStudio() and Enum.KeyCode.LeftShift or Enum.KeyCode.LeftControl
-- So explaining this. Lua always uses the last result. So if it is Studio then it will use LeftShift and if it's not studio it's LeftControl.

local PlayerCamera = game.Workspace.CurrentCamera
local OriginalFOV = 70
local SprintingFOV = 80

local function StartBarUpdate()
	local UITick
	UITick = game:GetService("RunService").RenderStepped:Connect(function()
		if Character.Parent == nil or Humanoid.Health == 0 then
			UITick:Disconnect()
		end
		StaminaText.Text = Energy.Value
		TweenService:Create(StaminaInnerFrame, TweenInfo.new(0.5 ,Enum.EasingStyle.Linear), {Size = UDim2.new(Energy.Value / 100, 0, 1, 0)}):Play()
	end)
	return UITick
end

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.KeyCode == ManStudioSucks then
		if Energy.Value >= 10 then
			isRunning = true
			TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 25}):Play()
			TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = SprintingFOV}):Play()
			while isRunning == true do
				if isRunning == false then
					TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
					break
				elseif Energy.Value <= 10 then
					if HRP:FindFirstChild("Speed Sparkles") then
						TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 20}):Play()
						TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
					else
						TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 16}):Play()
					end
					break
				else
					Energy.Value -= 1
					task.wait(Rate)
				end
			end
		else
			return
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, processed)
	if processed then return end
	if input.KeyCode == ManStudioSucks then
		isRunning = false
		if HRP:FindFirstChild("Speed Sparkles") then
			TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 20}):Play()
			TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
		else
			TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 16}):Play()
			TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
		end
		while isRunning == false do
			if isRunning == true or Energy.Value >= 100 then break end
			Energy.Value += 1
			task.wait(Rate)
		end
	end
end)

local UI = StartBarUpdate()
-- If you need to Disconnect UI for some reason, just remove the two lines below.
-- UI:Disconnect()

If this is your first time starting on programming, you’re doing a great job! Keep it up man don’t get lost in boredom land. Get some of that music pumping while making ur awesome stuff.

1 Like

I made it to left ctrl because of shift lock. Like if the players want to be in shift lock and sprint and then they press left shift then it’ll unshift lock them. I don’t want them getting annoyed lol

1 Like

I thought my own code looked messy so I tried making it look a bit cleaner they bought function the same though.

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")

local PlayerGui = Player:WaitForChild("PlayerGui")
local StaminaGui = PlayerGui:WaitForChild("StaminaGui")

local StaminaOuterFrame = StaminaGui:WaitForChild("StaminaOuterFrame")
local StaminaInnerFrame = StaminaOuterFrame:WaitForChild("StaminaInnerFrame")

local StaminaText = StaminaInnerFrame:WaitForChild("StaminaText")
local Energy = script:WaitForChild("Energy")

local Rate = 0.05
local isRunning = false

local PlayerCamera = game.Workspace.CurrentCamera
local OriginalFOV = 70
local SprintingFOV = 80

local function UpdateBar()
	while true do
		StaminaText.Text = Energy.Value
		TweenService:Create(StaminaInnerFrame, TweenInfo.new(0.5 ,Enum.EasingStyle.Linear), {Size = UDim2.new(Energy.Value / 100, 0, 1, 0)}):Play()
		task.wait(0.25)
	end
end

local function IsSprinting()
	TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 20}):Play()
	TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = SprintingFOV}):Play()
end

local function ResetSpeedAndCamera()
	if HRP:FindFirstChild("Speed Sparkles") then
		TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 20}):Play()
		TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {FieldOfView = OriginalFOV}):Play()
	else
		TweenService:Create(Humanoid, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {WalkSpeed = 16}):Play()
		TweenService:Create(PlayerCamera, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {FieldOfView = OriginalFOV}):Play()
	end
end

UserInputService.InputBegan:Connect(function(input, processed)
	if processed == true then return end
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if Energy.Value > 10 then
			isRunning = true
			IsSprinting()
			while isRunning == true do
				if isRunning == false or Energy.Value <= 10 then ResetSpeedAndCamera() break end
				Energy.Value -= 1
				task.wait(Rate)
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, processed)
	if processed == true then return end
	if input.KeyCode == Enum.KeyCode.LeftControl then
		isRunning = false
		ResetSpeedAndCamera()
		while isRunning == false do
			if isRunning == true or Energy.Value >= 100 then break end
			Energy.Value += 1
			task.wait(Rate)
		end
	end
end)
task.spawn(UpdateBar)

Anyways, I’m still encountering the same bug. It’s when you hold left ctrl FIRST and then try moving when the movement keys but it won’t let you.

1 Like

It only sets it to shift when you are playtesting in studio. In a game from the website it is changed to Left Control.

1 Like

Wait what? I don’t get it. What do you mean? Elaborate? :thinking:

1 Like

If you want to achieve true linear interpolation, you should use a “spring” module or a simplified version of one. If you spam the sprint key while walking, you will notice the speed of the tween varies depending on the proximity of the current position, to the goal position. This is because the tween is set to take n seconds regardless as opposed to using a function of distance. A spring module solves this issue. Here is a popular beefed up one by quenty: NevermoreEngine/Modules/Shared/Physics/Spring.lua at 5a429e871d54646ba54011c18321e77afa76d657 · Quenty/NevermoreEngine · GitHub. You can probably just fork the simpler code out of that if you want to keep stuff neat, or you can use the whole thing too. Its a good thing to learn about though, because you will see variations of this code everywhere in physics simulation.

In essence, you could implement this by creating a spring for walkspeed. Setting the speed and damper, and then just updating the spring’s goal whenever shift is pressed. Then you just have to read spring.position, for your values over some form of loop.

Regarding your initial question, is that solved now or are you still having trouble?

1 Like

Uhhhh… I don’t get it? The problem is that just when I hold Ctrl first and then just try to move it won’t let me but it can simply be fixed by releasing ctrl and try to move. I just find it really annoying when testing my own game.

1 Like

Does this only occur in studio, or in your published game too?

1 Like

What is a shift lock? I like my sprint to be on Q and sometimes I’ll use my arrow keys so I also like it to be Del. Most people are used to left Shift for Roblox so, My sprint key is Q, Del and left Shift. Ctrl is kind of off the table… I’ve seen right Ctrl and key pad 0 as lean to shoot keys. Never saw left Ctrl in a game however. Too may other programs use it as a tool type key.

1 Like

Shift lock is a default roblox feature that allows users to bind their shift key to lock their player orientation to their camera. Im sure theres some article about it out there.

1 Like

I know what you’re taking about now. I guess I have always set that to off.

1 Like

You know what I’m talking about! :smiley: but idk how to fix it tho

1 Like

Edit: I fixed it by changing the key code the left shift but I want the players to also be able to shift lock though

1 Like