Adding speed loop not working

I’m trying to make a script that adds you speed per second when you hold shift and if you release the button you will walk with 16 speed again but its not working.
here is the script!

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false
local speed = 40

function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end


mouse.KeyDown:connect(function (key) -- Run function
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
		end
		repeat wait () until running == false
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
		for i = 1,5 do
			
		end
	end
end)


while true do
	wait()
	if running == true then
		wait(0.2)
		speed = speed + 1
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
	else
		speed = 40
	end
end

Use UserInputService instead
and since you will need the connection(im assuming) you dont have to disconnect

UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input) -- once there is an input eg W or E or A or MouseClick
if input.KeyCode == Enum.KeyCode.LeftShift then --if it is leftshift
--ur code

end

end)

Thanks Bro i used your code to make and it worked!
any advices?

UserInputService = game:GetService("UserInputService")
local speed = 40
local running = false


UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = true
		speed = 40
	end
	
	UserInputService.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.LeftShift then
			running = false
		end
	end)
end)

while true do
	wait()
	if running == true then
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
		wait(0.2)
		speed = speed + 5
	else
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
	end
end

try not to use wait() in your code unless if really needed you can use task.wait()
Avoiding wait() and why - Resources / Community Tutorials - DevForum | Roblox
this post is very useful