Speed Gui not working

My script for sprinting works fine, but the gui does not work; when shift is pressed the gui has to go down but it works every other time. I have tried print statements to see where the error is or is the code flowing fully; resetting variables; changing math. Feel free to tell me if I need to add additional info.

(local) Script:

local player = game.Players.LocalPlayer
local char = player.Character
local rootPart = char:WaitForChild("Humanoid")
local walkSpeed = 16
local newWalkSpeed = 100
local max = 2  --(-1) because extra second is to be accounted for: 0,1,2
local cooldownTime = 2
local sprintFrame = script.Parent.SprintFrame
local progressBar = sprintFrame.Bar
local shift = false
local debounce = true
local startTime = 0
local newTime = 0
local averageTime = 0

function newSpeed()
	char.Humanoid.WalkSpeed = newWalkSpeed
end
------------------------------------------------
function oldSpeed()
	char.Humanoid.WalkSpeed = walkSpeed
end
------------------------------------------------
--progressBar:TweenSize(UDim2.new(0,0,1,0), "Out", "Linear", max + 1) 
--(+1) because extra second is to be accounted for: 0,1,2
------------------------------------------------
function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		shift = true
	end		
end
------------------------------------------------
function onKeyRelease(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		shift = false
	end
end
------------------------------------------------
function resetVars()
	startTime = 0
	newTime = 0
	averageTime = 0
end
------------------------------------------------
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyRelease)
------------------------------------------------

while true do
	wait(0.01)
	if shift == true then
		resetVars()
		if debounce == true then
			startTime = tick()
			newSpeed()
			print(1)
			repeat
				print(2)
				newTime = tick()
				averageTime = math.abs(startTime - newTime)
				progressBar:TweenSize(UDim2.new((0.7 - (averageTime/max)),0,1,0), "Out", "Linear", 0.5)
				wait() 
			until shift == false or averageTime > max
			print(3)
			if averageTime > max then
				print(4)
				oldSpeed()
				shift = false
				debounce = false
			end
		end
	end
	
	if shift == false then
		if averageTime > max then
			if debounce == false then
				oldSpeed()
				progressBar:TweenSize(UDim2.new(1,0,1,0), "Out", "Linear", cooldownTime+2)
				print(6)
				if progressBar.Size == UDim2.new(1,0,1,0) then
					print(7)
					debounce = true
					resetVars()
					shift = false
					print(8)
				end
			end
		else
			oldSpeed() 
			progressBar:TweenSize(UDim2.new(1,0,1,0), "Out", "Linear", 0.5)
			debounce = true
		end
	end
end

Video:
robloxapp-20200807-1316260.wmv (1.6 MB)

1 Like