Sprintscript is weird

So i want to do a sprinting with stamina,

but the thing is i wanna detect if the player is pressing W/A/S/D with shift so the stamina doesn’t waste without any reason, but if i press shift 1st and then W/A/S/D i cant sprint

I thought it has something to do with the InputBegan function so i tried to experiment…
nothing did work

If you can give me tips about stamina i would be thankfull!

local ViewModel = game.ReplicatedStorage.Models:WaitForChild("Viewmodel")
local SprintAnimation = game.ReplicatedStorage.Animations:WaitForChild("Sprinthold")
local Module = require(game.ReplicatedStorage.Scripts.MainShotgun)
local UIS = game:GetService("UserInputService")
local Stamina = 200
local StaminaActive = false
local StaminaUsing = false
local Exhausted = false
local W = false
local A = false
local S = false
local D = false
local Moving = false -- should detect if the player moves using wasd
local Sprinting = false 
local using = false 



function StartStamina() --is starting the stamina wasting
	while Stamina > 0 and StaminaActive == true do
		Stamina = Stamina - 1
		if Stamina == 0 then
			Exhausted = true
			SprintStop()
			return
		end
		wait(0.01)

		print(Stamina)


	end
end

local function StaminaReset() --is stopping the current stamina wasting

	if StaminaActive == false then
		StaminaUsing = true
		StaminaActive = true
		StartStamina()
	end
end

character = Player.CharacterAdded:Connect(function(character)
	StaminaActive = false




	Stamina = 200-- Max Stamina in Seconds
	if StaminaUsing == false then 
		while Stamina <= 199 do
			Stamina = Stamina + 1
			print(Stamina)

			
			
			wait(0.01)
		end
	end

	UIS.InputBegan:Connect(function(inputM)
		if inputM.KeyCode == Enum.KeyCode.W then
			W = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		if inputM.KeyCode == Enum.KeyCode.A then
			A = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		if inputM.KeyCode == Enum.KeyCode.S then
			S = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		if inputM.KeyCode == Enum.KeyCode.D then
			D = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		UIS.InputEnded:Connect(function(inputW)
			if inputW.KeyCode == Enum.KeyCode.W then
				W = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
		end)
		UIS.InputEnded:Connect(function(inputA)
			if inputA.KeyCode == Enum.KeyCode.A then
				A = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
		end)
		UIS.InputEnded:Connect(function(inputS)
			if inputS.KeyCode == Enum.KeyCode.W then
				S = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
		end)
		UIS.InputEnded:Connect(function(inputD)
			if inputD.KeyCode == Enum.KeyCode.W then
				D = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
			if inputD.KeyCode == Enum.KeyCode.LeftShift then
				Sprinting = false 
				Module.SprintStop()
				character.Humanoid.WalkSpeed = 16
				SprintStop()

			end
		end)



	end)
	UIS.InputBegan:Connect(function(input) 


		if input.KeyCode == Enum.KeyCode.LeftShift then	
			Sprinting = true	
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()
						return
					end



				end		

			end
		end	

		return
	end)
	UIS.InputEnded:Connect(function(inputEnd) --should cancel it (it doesn't)

		
	end)






	function SprintStop()
		using = false
		StaminaActive = false
		StaminaUsing = false
		Module.SprintStop()
		character.Humanoid.WalkSpeed = 16
	end

	return

end)